Bird
0
0

What is the output of this Ruby code?

medium📝 Predict Output Q4 of 15
Ruby - Class Methods and Variables
What is the output of this Ruby code?
class String
  def shout
    self.upcase + "!"
  end
end

puts "hello".shout
Ahello!
BHELLO!
CHello!
DError: method shout undefined
Step-by-Step Solution
Solution:
  1. Step 1: Understand the reopened String class

    The shout method returns the string in uppercase plus an exclamation mark.
  2. Step 2: Apply the method to "hello"

    Calling "hello".shout returns "HELLO!".
  3. Final Answer:

    HELLO! -> Option B
  4. Quick Check:

    Reopened method output = HELLO! [OK]
Quick Trick: Reopened methods work like normal instance methods [OK]
Common Mistakes:
  • Expecting lowercase output
  • Thinking method is undefined

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes