0
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

我得到一个无方法错误,为什么?

4

1 回答 1

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”

于 2012-04-27T23:29:19.383 回答