Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Blocks, Procs, and Lambdas
What is the output of this Ruby code?
class Greeter
  def hello(name)
    "Hello, #{name}!"
  end
end

g = Greeter.new
m = g.method(:hello)
puts m.call("Alice")
AError: undefined method
BHello, Alice!
Chello
D#<Method: Greeter#hello>
Step-by-Step Solution
Solution:
  1. Step 1: Understand the code flow

    A Greeter object is created, then a Method object for :hello is obtained and called with "Alice".
  2. Step 2: Evaluate the method call output

    The method returns the string "Hello, Alice!" which is printed.
  3. Final Answer:

    Hello, Alice! -> Option B
  4. Quick Check:

    Method call output = Hello, Alice! [OK]
Quick Trick: Method objects call returns method result, not method itself [OK]
Common Mistakes:
  • Expecting the Method object string instead of method result
  • Confusing method name with output
  • Assuming error without checking method existence

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes