timberlake timberlake - 4 months ago 8
Ruby Question

Ruby (NoMethodError) even when method is declared

I am getting my hands dirty learning ruby the hard way.
I am stuck at lesson 26. Here is the link to the file. And this is the code snippet where the error lies:

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)


this is the error on the console

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

Answer

There are a couple of typos in the variable declarations. First you have jelly_bens (missing an "a") instead of jelly_beans. Second, you have crate (singular) instead of crates (plural). Once those are fixed, the code executes properly:

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)