Bird
0
0

What will be the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Class Methods and Variables
What will be the output of this Ruby code?
class Dog
  def self.bark
    "Woof!"
  end
end

puts Dog.bark
Anil
BWoof!
CNoMethodError
Dbark
Step-by-Step Solution
Solution:
  1. Step 1: Understand the class method call

    The method bark is defined as a class method using self.bark and returns the string "Woof!".
  2. Step 2: Check the output of puts Dog.bark

    Calling Dog.bark returns "Woof!", which puts prints to the console.
  3. Final Answer:

    Woof! -> Option B
  4. Quick Check:

    Class method returns string printed = Woof! [OK]
Quick Trick: Class methods return values accessible via ClassName.method [OK]
Common Mistakes:
  • Trying to call class method on an instance
  • Expecting method name as output instead of return value
  • Confusing NoMethodError with correct method call

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes