为什么我会得到NoM​​ethodError?

时间:2012-04-27 23:24:30

标签: ruby function methods

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

我得到一个没有方法的错误,为什么?

1 个答案:

答案 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”