Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRob Castellucci
10,268 PointsAfter methods are defined, can someone explain how code is read and what is happening in each step?
I understand the defining of methods. After this, I don't understand the underlying interpreter steps for name = get_name() for instance and the other method calls.
4 Answers
Lee Parham
3,173 PointsSo, these are variables and they are assigned data from the return of the function.
The first one:
name = get_name()
runs the get_name method. That asks for your name and returns a string based on your input. This string is what gets assigned to the variable "name"
That variable is used in the next call
greet(name)
it would be the same as if you called the function like:
greet("Rob")
name is a string and you call the greet function with the variable that has a string as the value.
number = get_number()
is the same as get_name(), except the ".to_i" sends an integer value to the variable "number".
the final statement:
if divisible_by_3?(number)
will call the function. If you look at the function, it returns either true or false. so it could be read: if "true" do the inside block. If "false" do nothing.
Rob Castellucci
10,268 PointsThanks! I got tripped up with the first one. I didn't realize that name = get_name() actually executed the get_name function at that time. But now it makes sense.
Lee Parham
3,173 PointsSure thing! Will you mark the response as answered?
Rob Castellucci
10,268 PointsI'm new to asking questions. I hit "best answer" under yours. Is that same thing?
Lee Parham
3,173 PointsI'm new to responding. I think so. Thanks!
Alexander Schieving
3,800 PointsGreat question and great answer. Deepens understanding this way!