Bird
0
0

Which of the following is the correct syntax to define and use a class variable in Ruby?

easy📝 Syntax Q12 of 15
Ruby - Class Methods and Variables
Which of the following is the correct syntax to define and use a class variable in Ruby?
Aclass MyClass @@count = 0 end
Bclass MyClass @count = 0 end
Cclass MyClass $count = 0 end
Dclass MyClass count = 0 end
Step-by-Step Solution
Solution:
  1. Step 1: Identify class variable syntax

    Class variables start with @@, so @@count = 0 is correct for defining a class variable.
  2. Step 2: Differentiate other variable types

    @count is an instance variable, $count is a global variable, and count alone is a local variable.
  3. Final Answer:

    class MyClass @@count = 0 end -> Option A
  4. Quick Check:

    Class variable syntax uses @@ [OK]
Quick Trick: Class variables always start with @@ [OK]
Common Mistakes:
  • Using @ instead of @@ for class variables
  • Confusing global variables ($) with class variables
  • Defining variables without any prefix

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes