参数数量错误(0表示2)

时间:2013-09-11 20:27:10

标签: ruby

我目前正在研究一个小型学校项目(Ruby),但是我收到了这个错误(错误的参数数量(0表示为2))这让我想要翻转一张桌子。 :(

这是我试图开始工作的代码:

puts "How much does the product cost?"
price = gets.to_f.round

puts "How much money will you give for it?"
money = gets.to_f

change = calculate_change(price, money)

我用它来获取用户输入,将第一个转到fixnum,然后将第二个转到float。这就是我的calculate_change方法的样子:

def calculate_change(price, money)
    return money - price
end

1 个答案:

答案 0 :(得分:2)

Per @hirolau的评论,您需要确保在调用之前声明calculate_change()

def calculate_change(price, money)
    money - price                    # return is optional in Ruby!
end

puts "How much does the product cost?"
price = gets.to_f.round

puts "How much money will you give for it?"
money = gets.to_f

change = calculate_change(price, money)