An interactive Ruby lesson
Local scope
A method definition creates a separate local scope. Build on Variables and Methods to pass values into a method and return a result.
p displays an inspected value, with quotes around strings. Predict the output, press Run, then open the expected output to check your answer. Edit the code and run it again. Reset restores that example; Stop ends a running example.
The first Run downloads Ruby into your browser. JavaScript and a network connection are needed to run examples. Each run starts fresh, so define every variable in the editor you run. The code and answers remain readable without JavaScript.
1. Separate local names
The two variables named count belong to separate scopes. The count inside show_count is local to that method. The caller is the code that calls the method; its count stays at 10. Predict the line from the method and the line from the caller.
Show expected output
2 10
Try it: Change the caller’s count = 10 to count = 20. Predict which output line changes. Then reset and change the method’s count = 2 to count = 3.
2. Check availability
A method cannot read the caller’s local variables just by using their names. Here outside_total is available only in the caller’s scope.
The keyword defined? describes whether an expression is available. For these names, it returns the string "local-variable" or nil. It does not return the stored value or a Boolean. Use parentheses around its argument: defined?(outside_total). Predict the result in each scope.
Show expected output
"local-variable" nil
Try it: Change outside_total = 8 to outside_total = 0. Predict whether either description changes.
3. Pass an argument
A call passes an argument to a method. Here the value of amount becomes available through the parameter received_amount. That parameter is a local variable inside show_amount. Its name does not become available in the caller. Predict both lines.
Show expected output
10 nil
Try it: Change amount = 10 to amount = 25. Predict the displayed value and whether the parameter name becomes available outside the method.
4. Reassign a parameter
The parameter and caller’s variable are both named number, but they are separate local names. Here number += 2 calculates a new integer and assigns it to the method’s parameter. The caller’s number still refers to 5. Predict both values.
This example shows local reassignment with integers. Passing an argument does not make a copy of its object.
Show expected output
7 5
Try it: Change the caller’s number = 5 to number = 0. Predict the value displayed inside the method and the value displayed after the call.
5. Initialize each call
Each call runs count = 0 before adding one. The second call starts its local count at zero again, so this method cannot keep a running counter across calls. The last expression, count, supplies the return value. Predict both lines.
Show expected output
1 1
Try it: Change count = 0 to count = 4. Predict both returned values.
6. Return a result
price, count, and subtotal are local to calculate_total. Its last expression returns the total. The caller stores that returned value in its own variable, result. Returning the value does not make the name subtotal available to the caller. Predict both lines.
Show expected output
14 nil
Try it: Change the call to calculate_total(3, 0). Predict the result and whether subtotal becomes available in the caller.
Practice on your own
Replace the code in the last editor with a program for these tasks. Predict the output, then run it.
- Define
add_points(starting). Inside it, assignstarting + 3to a local variable namedupdated, then returnupdatedas the last expression. - In the caller, set
starting = 4and storeadd_points(starting)intotal. - Use
pto displaytotal,starting, anddefined?(updated), in that order. - Explain where each name is available. Predict all three lines if the caller starts at
0.
Show one solution and its output
def add_points(starting) updated = starting + 3 updated end starting = 4 total = add_points(starting) p(total) p(starting) p(defined?(updated))
Output:
7 4 nil
Ruby documentation
The official Ruby FAQ explains separate local scopes and argument passing. The Ruby 4.0 documentation covers method arguments, return values, and defined?.
Return to the Ruby skill tree to choose another topic.