Bird
Raised Fist0
Rubyprogramming~10 mins

Method objects with method() in Ruby - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Method objects with method()
Define a method
↓
Call method() on object
↓
Get Method object
↓
Call .call on Method object
↓
Execute original method
↓
Return result
This flow shows how calling method() on an object returns a Method object, which can be called later to run the original method.
Execution Sample
Ruby
class Greeter
  def hello
    "Hello!"
  end
end

g = Greeter.new
m = g.method(:hello)
puts m.call
This code creates a Method object for the 'hello' method and calls it to print 'Hello!'.
Execution Table
StepActionObject StateMethod ObjectOutput
1Create Greeter instance gg = #<Greeter:0x000...>nilnil
2Call g.method(:hello)g unchangedMethod object for hello bound to gnil
3Call m.callg unchangedMethod object still exists"Hello!" printed
4End of programNo changeNo changeProgram ends
💡 Program ends after printing 'Hello!' from the Method object call
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
gnil#<Greeter:0x000...>#<Greeter:0x000...>#<Greeter:0x000...>#<Greeter:0x000...>
mnilnilMethod(:hello) bound to gMethod(:hello) bound to gMethod(:hello) bound to g
Key Moments - 3 Insights
Why do we need to call .call on the Method object?
The method() call returns a Method object, which is like a saved reference to the method. To actually run the method, we must call .call on that Method object, as shown in step 3 of the execution_table.
Does calling method() execute the method immediately?
No, calling method() only returns a Method object without running the method. The method runs only when .call is used on that Method object, as seen between steps 2 and 3.
Is the original object changed after getting the Method object?
No, the original object remains unchanged throughout. The Method object just holds a reference to the method bound to that object, as shown in the Object State column.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2. What does g.method(:hello) return?
Anil
BThe string 'Hello!'
CA Method object bound to g
DAn error
💡 Hint
Check the 'Method Object' column at step 2 in the execution_table.
At which step is the original 'hello' method actually executed?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for when the output 'Hello!' is printed in the execution_table.
If we skip calling m.call, what will be the output?
Anil
BMethod object printed
CHello!
DError
💡 Hint
Refer to the Output column in the execution_table for step 2 and 3.
Concept Snapshot
Use method(:name) on an object to get a Method object.
Call .call on that Method object to run the original method.
method() does NOT run the method immediately.
The Method object keeps the method bound to the original object.
Useful to store or pass methods as objects.
Full Transcript
This visual trace shows how Ruby's method() works. First, an object is created from a class with a method. Calling method(:method_name) on the object returns a Method object, which is a reference to that method bound to the object. This does not run the method yet. To run it, you call .call on the Method object. The output appears only after .call is used. The original object remains unchanged throughout. This lets you save methods as objects and call them later.

Practice

(1/5)
1. What does the Ruby expression obj.method(:foo) return?
easy
A. A Method object representing the method foo of obj
B. The result of calling foo on obj
C. A symbol :foo
D. An error because method is not a valid method

Solution

  1. Step 1: Understand method usage

    The method method on an object returns a Method object representing the named method.
  2. Step 2: Identify the return type

    Calling obj.method(:foo) returns a Method object, not the method's result or a symbol.
  3. Final Answer:

    A Method object representing the method foo of obj -> Option A
  4. Quick Check:

    obj.method(:foo) returns Method object [OK]
Hint: Remember: method returns a Method object, not the call result [OK]
Common Mistakes:
  • Thinking it calls the method immediately
  • Confusing with symbol :foo
  • Assuming it returns a string or error
2. Which of the following is the correct syntax to get a Method object for the method greet on object person?
easy
A. person.send(:greet)
B. person.method[b greet]
C. person.method(greet)
D. person.method(:greet)

Solution

  1. Step 1: Check argument type for method

    The method method expects a symbol representing the method name.
  2. Step 2: Validate options

    person.method(:greet) uses a symbol :greet, which is correct. person.send(:greet) calls the method directly, returning its result instead of a Method object. person.method(greet) passes an undefined variable greet, causing error. person.method[b greet] uses brackets, which is invalid syntax.
  3. Final Answer:

    person.method(:greet) -> Option D
  4. Quick Check:

    Use symbol argument with method [OK]
Hint: Use a symbol like :greet inside method() [OK]
Common Mistakes:
  • Confusing method() with send()
  • Using brackets instead of parentheses
  • Passing undefined variable instead of symbol
3. What is the output of the following Ruby code?
class Calculator
  def add(x, y)
    x + y
  end
end
calc = Calculator.new
adder = calc.method(:add)
puts adder.call(5, 7)
medium
A. 5 7
B. 12
C. add
D. NoMethodError

Solution

  1. Step 1: Understand method object creation

    The method(:add) returns a Method object for add on calc.
  2. Step 2: Calling the Method object

    Calling adder.call(5, 7) executes add(5, 7), which returns 12.
  3. Final Answer:

    12 -> Option B
  4. Quick Check:

    Method object call returns sum 12 [OK]
Hint: Use call on Method object to run method with arguments [OK]
Common Mistakes:
  • Expecting method name or arguments printed
  • Confusing output with error
  • Not using call to invoke method
4. Identify the error in this Ruby code snippet:
class Person
  def name
    "Alice"
  end
end
p = Person.new
m = p.method(name)
m.call
medium
A. Missing quotes around method name in method(name)
B. Cannot call method on an object
C. Method name does not exist
D. Using call on Method object is invalid

Solution

  1. Step 1: Check argument to method

    The argument name is used as a variable, but it should be a symbol or string representing the method name.
  2. Step 2: Correct usage

    It should be p.method(:name) or p.method('name') to get the Method object.
  3. Final Answer:

    Missing quotes around method name in method(name) -> Option A
  4. Quick Check:

    Method name must be symbol or string [OK]
Hint: Always pass method name as symbol or string to method() [OK]
Common Mistakes:
  • Passing bare method name without quotes or colon
  • Thinking method is invalid on objects
  • Confusing call usage
5. Given the following code, what will be the output?
class Greeter
  def initialize(name)
    @name = name
  end
  def greet
    "Hello, #{@name}!"
  end
end

g1 = Greeter.new("Bob")
g2 = Greeter.new("Sue")
m1 = g1.method(:greet)
m2 = g2.method(:greet)

puts m1.call
puts m2.call
hard
A. Hello, Sue!\nHello, Sue!
B. Hello, Bob!\nHello, Bob!
C. Hello, Bob!\nHello, Sue!
D. Error: undefined method greet

Solution

  1. Step 1: Understand instance variables and method objects

    Each Greeter instance stores its own @name. The method(:greet) returns a Method object bound to that instance.
  2. Step 2: Calling Method objects

    Calling m1.call runs greet on g1, printing "Hello, Bob!". Similarly, m2.call prints "Hello, Sue!".
  3. Final Answer:

    Hello, Bob! Hello, Sue! -> Option C
  4. Quick Check:

    Method objects keep instance context [OK]
Hint: Method objects remember their object, so output matches instance data [OK]
Common Mistakes:
  • Assuming method objects lose instance context
  • Expecting same output for both calls
  • Confusing method object with class method
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard