It returns a Method object that represents a method bound to a specific object. This allows you to store, pass, or call the method later.
Click to reveal answer
beginner
How do you call a method stored in a Method object?
You use the call method on the Method object. For example, m = obj.method(:foo); m.call calls the foo method on obj.
Click to reveal answer
intermediate
Can you pass a Method object as a block to another method?
Yes! You can convert a Method object to a block using the & operator. For example, array.map(&obj.method(:foo)) passes the method as a block.
Click to reveal answer
intermediate
What is the difference between method() and send() in Ruby?
method() returns a Method object without calling it immediately, while send() calls the method immediately on the object.
Click to reveal answer
beginner
Why might you want to use Method objects instead of calling methods directly?
Method objects let you store methods as variables, pass them around, and call them later. This helps write flexible and reusable code.
Click to reveal answer
What does obj.method(:foo) return in Ruby?
ARaises an error
BCalls the method <code>foo</code> on <code>obj</code> immediately
CReturns the symbol <code>:foo</code>
DA Method object representing the method <code>foo</code> bound to <code>obj</code>
✗ Incorrect
The method() function returns a Method object bound to the object, not the result of calling the method.
How do you invoke a Method object stored in variable m?
A<code>m.call</code>
B<code>m.invoke()</code>
C<code>m.execute()</code>
D<code>m.run()</code>
✗ Incorrect
You call the method stored in a Method object using call.
Which operator converts a Method object to a block in Ruby?
A#
B&
C$
D*
✗ Incorrect
The & operator converts a Method object to a block for passing to methods like map.
What is the main advantage of using Method objects?
AThey allow storing and passing methods as objects
BThey speed up method execution
CThey automatically memoize results
DThey replace instance variables
✗ Incorrect
Method objects let you treat methods like data, storing and passing them around.
What happens if you call obj.method(:foo).call?
ARaises an error
BReturns the Method object without calling
CCalls the <code>foo</code> method on <code>obj</code>
DReturns <code>nil</code>
✗ Incorrect
Calling call on the Method object invokes the method on the original object.
Explain how to create and use a Method object in Ruby.
Think about how you get a method as an object and then run it.
You got /4 concepts.
Describe a practical scenario where using Method objects is helpful.
Consider when you want to pass a method to another method like map or each.
You got /4 concepts.
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
Step 1: Understand method usage
The method method on an object returns a Method object representing the named method.
Step 2: Identify the return type
Calling obj.method(:foo) returns a Method object, not the method's result or a symbol.
Final Answer:
A Method object representing the method foo of obj -> Option A
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
Step 1: Check argument type for method
The method method expects a symbol representing the method name.
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.
Final Answer:
person.method(:greet) -> Option D
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
Step 1: Understand method object creation
The method(:add) returns a Method object for add on calc.
Step 2: Calling the Method object
Calling adder.call(5, 7) executes add(5, 7), which returns 12.
Final Answer:
12 -> Option B
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
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.
Step 2: Correct usage
It should be p.method(:name) or p.method('name') to get the Method object.
Final Answer:
Missing quotes around method name in method(name) -> Option A
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
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.
Step 2: Calling Method objects
Calling m1.call runs greet on g1, printing "Hello, Bob!". Similarly, m2.call prints "Hello, Sue!".
Final Answer:
Hello, Bob!
Hello, Sue! -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.