But when I tried to extend it to (A+B)/C, it display error message :
def add(a, b)
puts "(ADDING #{a} + #{b}) / #{c}"
return (a + b) / c
end
age = add(30, 10) / 5
puts "Age: #{age}"
Display :
CaiGengYangs-MacBook-Pro:GoatBoy CaiGengYang$ ruby ex90.rb
ex90.rb:2:in `add': undefined local variable or method `c' for main:Object (NameError)
from ex90.rb:6:in `<main>''
def add(a, b, c)
puts "(ADDING #{a} + #{b}) / #{c}"
return (a + b) / c
end
age = add(30, 10) / 5
puts "Age: #{age}"
Then I tried to run it : CaiGengYangs-MacBook-Pro:~ CaiGengYang$ ruby ex50.rb
But I got this error message : ruby: No such file or directory -- ex50.rb (LoadError)
For your Ruby code, you are defining a method add which takes three arguments, a, b, and c, however when you are calling it, you only supply two arguments (30 and 10). You need to give it three arguments. It seems as if you line:
age = add(30, 10) / 5
should have been
age = add(30, 10, 5)
instead, as it seems that you want the result to be (30 + 10) / 5 (which results to 8)