2012-04-27 122 views
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}) 

當我嘗試用說運行它,爲什麼我得到一個NoMethodError?

@number = 30 
@eqn = sin 

我得到一個無方法的錯誤,爲什麼呢?

回答

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吧。 如果你不chomp,你會最終調用方法「罪\ n」而不是「罪」

相關問題