A method is a named, reusable chunk of code: define it once with def, call it as many times as you like.
1 def names it
def greet ... end stores the code under a name — nothing runs until you call it.
2 Parameters catch input
Names between the parentheses catch the arguments you pass in at call time.
3 Last line returns
No return keyword needed — the final expression of the body is the result.
4 Parentheses optional
greet("Ada") and greet "Ada" both work — Ruby style prefers ( ) on methods with arguments.
Name a chunk of code
def names it1def greet ... end stores the code under a name — nothing runs until you call it. Parameters catch input2Names between the parentheses catch the arguments you pass in at call time. Last line returns3No return keyword needed — the final expression of the body is the result.
Parentheses optional4greet("Ada") and greet "Ada" both work — Ruby style prefers ( ) on methods with arguments. 4 building blocks make up Name a chunk of code
Watch control hop into a method body and back — twice, with different arguments.
maindef greet stores the method — Ruby skips over the body until someone calls it.
Memory
no variables yet — press play
Console
output appears as the program runs…
Every method call pushes a frame onto the stack and every return pops it. Play the script, then drag to orbit.
Drag to orbit the scene. Press Play to watch memory in action.
Click a term, then click its job to lock the pair.
5 conceptsTap a card to expand ?def namedrops into place
?parameterdrops into place
?return valuedrops into place
?greet("Ada")drops into place
?puts methoddrops into place
Methods calling methods — evaluate from the inside out.
Your predicted output — one line per console line
Hint: double(double(2)) runs inside-out: double(2) is 4, then double(4) is 8. And shout glues "!" onto the upcased word before returning it.
01def stores a method; calling it by name runs the body.
02Parameters catch arguments; the last expression is the return value.
03Each call pushes a stack frame; each return pops it.
04One method can call another — double(double(2)) runs inside-out.
05puts prints whatever the method returned.