Reputation: 171
def secret_formala(PH)
jelly_beans = PH * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates
end
start_point = 10000
beans, jars, crates = secret_formala(start_point)
puts "With a starting point of: #{start_point}"
puts "We'd have #{beans} beans, #{jars} jars, and #{crates} crates."
start_point = start_point / 10
puts "We can also do that this way:"
puts "We'd have %s beans, %s jars, and %s crates." % secret_formala(start_point)
puts
So, here is my code that I have and my confusion, which may seem really obvious to others but since I am still pretty new at Ruby, it escapes me. What I don't understand is Line 1 & 2 the "def secret_formula(PH)" and the "jelly_beans = PH * 500"
I can put anything in place of "PH" and it works, but how did the code understand that the places I have the "PH" are using the "start_point" numbers? Why did I not get an error? What is the point of putting anything in "()" after the "def secret_formula(PH) in line 1?
Upvotes: 1
Views: 70
Reputation: 175
def secret_formala(PH)
The def keyword is what determines if this is a method in ruby or not. It ends with the end
keyword.
What goes in the parens, in this case PH, that is what you are passing in to the method when you call it. For example if you called it as secret_formula(10)
, then the next line would read jelly_beans = 10 * 500
internally in the code. You do not always need to have the parens there in the method definition. This is because you might not be passing in another value into this method always. You might have a method that just does something internally without any additional info given to it. PH itself does not really matter here, it is just the name you are using for the value you are passing it. You could name it 'hello' if you desired.
Upvotes: 2