Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Classes and Objects
What will be the output of this Ruby code?
class Cat
  def initialize(name)
    @name = name
  end
  def show_self
    self
  end
end
c = Cat.new('Milo')
puts c.show_self
Anil
B#<Cat:0x00007fffe40b8a88>
CCat
DMilo
Step-by-Step Solution
Solution:
  1. Step 1: Understand what show_self returns

    The method show_self returns self, which is the current instance of Cat.
  2. Step 2: What does puts print for an object

    Printing an object prints its default string representation, like #<ClassName:object_id>.
  3. Final Answer:

    #<Cat:0x00007fffe40b8a88> -> Option B
  4. Quick Check:

    Instance self prints object reference [OK]
Quick Trick: Printing self in instance method shows object info [OK]
Common Mistakes:
  • Expecting instance variable value
  • Expecting class name
  • Expecting nil

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes