class UnitCircle
def prompt
puts "Enter a number: "
@number = gets
@number = @number.to_i
puts "Enter a trigonometric equation to perform on that number: "
@eqn = gets
end
end
uc = UnitCircle.new
uc.prompt
num = Math.send(uc.instance_eval {@eqn}, uc.instance_eval {@number})
当我尝试用它来运行时,
@number = 30
@eqn = sin
我得到一个没有方法的错误,为什么?
答案 0 :(得分:0)
class UnitCircle
def prompt
puts "Enter a number: "
@number = gets.chomp
@number = @number.to_i
puts "Enter a trigonometric equation to perform on that number: "
@eqn = gets.chomp
end
end
gets
方法将包含新的行符号“\ n”。所以你必须chomp
它。
如果你没有选择,你最终会调用方法“sin \ n”而不是“sin”