gpt4 book ai didi

Ruby (NoMethodError) 即使声明了方法

转载 作者:太空宇宙 更新时间:2023-11-03 18:12:44 28 4
gpt4 key购买 nike

我正在努力学习 ruby​​。我卡在第 26 课了。Here是文件的链接。这是错误所在的代码片段:

five = 10 - 2 + 3 - 6
puts "This should be five: #{five}"

def secret_formula(started)
jelly_bens = started * 500
jars = jelly_beans / 1000
crate = jars / 100
return jelly_beans, jars, crates
end

start_point = 10000
beans, jars, crates = secret_formula(start_point)

这是控制台的错误

ex26.rb:77:in `<module:Ex25>': undefined method `secret_formula' for     Ex25:Module (NoMethodError)
from ex26.rb:1:in `<main>'

最佳答案

变量声明中有几个拼写错误。首先,您有 jelly_bens(缺少“a”)而不是 jelly_beans。其次,您有 crate(单数)而不是 crates(复数)。修复这些问题后,代码将正确执行:

five = 10 - 2 + 3 - 6
puts "This should be five: #{five}"

def secret_formula(started)
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
end

start_point = 10000
beans, jars, crates = secret_formula(start_point)

关于Ruby (NoMethodError) 即使声明了方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31340396/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com