Bird
0
0

What will this Ruby code print?

medium📝 Predict Output Q5 of 15
Ruby - Class Methods and Variables
What will this Ruby code print?
class Example
  @value = 5
  def self.value
    @value
  end
end

puts Example.value
A5
Bnil
CError
D0
Step-by-Step Solution
Solution:
  1. Step 1: Understand class instance variable

    @value defined on class itself is a class instance variable accessible by class methods.
  2. Step 2: Call class method

    Example.value returns @value which is 5.
  3. Final Answer:

    5 -> Option A
  4. Quick Check:

    Class instance variable accessed by class method = 5 [OK]
Quick Trick: Class instance variables use single @ in class methods [OK]
Common Mistakes:
  • Confusing class instance variables with class variables
  • Expecting nil because @value looks like instance variable
  • Thinking @value is undefined

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes