Bird
0
0

Which of the following is the correct syntax to set an instance variable @title to "Developer" using instance_variable_set on object obj?

easy📝 Syntax Q3 of 15
Ruby - Metaprogramming Fundamentals
Which of the following is the correct syntax to set an instance variable @title to "Developer" using instance_variable_set on object obj?
Aobj.instance_variable_set(:title, "Developer")
Bobj.instance_variable_set("title", "Developer")
Cobj.instance_variable_set(:@title, "Developer")
Dobj.instance_variable_set(@title, "Developer")
Step-by-Step Solution
Solution:
  1. Step 1: Understand argument format for instance_variable_set

    The first argument must be the instance variable name as a symbol or string, including the '@' prefix.
  2. Step 2: Check each option

    obj.instance_variable_set(:title, "Developer") misses '@'. obj.instance_variable_set("title", "Developer") misses '@'. obj.instance_variable_set(:@title, "Developer") uses :@title symbol correctly. obj.instance_variable_set(@title, "Developer") passes a variable without quotes or symbol, which is invalid.
  3. Final Answer:

    obj.instance_variable_set(:@title, "Developer") -> Option C
  4. Quick Check:

    Use symbol with '@' for instance_variable_set [OK]
Quick Trick: Prefix variable name with '@' and use symbol for setting [OK]
Common Mistakes:
  • Omitting '@' in variable name
  • Passing variable name without quotes or symbol
  • Using string without '@' prefix

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes