Bird
0
0

Which of the following is the correct way to define an instance variable inside a Ruby class?

easy📝 Syntax Q12 of 15
Ruby - Classes and Objects
Which of the following is the correct way to define an instance variable inside a Ruby class?
Avar @name = 'Alice'
B@name = 'Alice'
Cname = @'Alice'
Ddef @name = 'Alice'
Step-by-Step Solution
Solution:
  1. Step 1: Understand instance variable assignment syntax

    In Ruby, instance variables are assigned by writing @variable_name = value inside methods or constructors.
  2. Step 2: Check each option

    @name = 'Alice' correctly assigns an instance variable. The other options use invalid syntax.
  3. Final Answer:

    @name = 'Alice' -> Option B
  4. Quick Check:

    Assign instance variable with @name = value [OK]
Quick Trick: Assign instance variables with @name = value inside methods [OK]
Common Mistakes:
  • Adding var keyword (not used in Ruby)
  • Placing @ inside quotes
  • Using def keyword incorrectly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes