Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q13 of 15
Ruby - Metaprogramming Fundamentals
What is the output of this Ruby code?
class Person
  def greet(name)
    "Hello, #{name}!"
  end
end

p = Person.new
puts p.send(:greet, "Alice")
AHello, Alice!
Bgreet
CNoMethodError
DHello, name!
Step-by-Step Solution
Solution:
  1. Step 1: Understand the method call with send

    The send method calls greet on p with argument "Alice".
  2. Step 2: Evaluate the method output

    The greet method returns "Hello, Alice!" which is printed by puts.
  3. Final Answer:

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

    send calls greet with argument "Alice" [OK]
Quick Trick: send passes arguments after method name [OK]
Common Mistakes:
  • Expecting method name string as output
  • Confusing argument passing with no arguments
  • Thinking send returns method object

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes