Ruby - Classes and ObjectsWhich 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'Check Answer
Step-by-Step SolutionSolution:Step 1: Understand instance variable assignment syntaxIn Ruby, instance variables are assigned by writing @variable_name = value inside methods or constructors.Step 2: Check each option@name = 'Alice' correctly assigns an instance variable. The other options use invalid syntax.Final Answer:@name = 'Alice' -> Option BQuick 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 quotesUsing def keyword incorrectly
Master "Classes and Objects" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Classes and Objects - Initialize method as constructor - Quiz 14medium Classes and Objects - To_s method for string representation - Quiz 3easy Enumerable and Collection Processing - Select/filter for filtering - Quiz 8hard Error Handling - Raise for throwing errors - Quiz 1easy Error Handling - Exception hierarchy - Quiz 14medium File IO - IO modes (r, w, a) - Quiz 6medium Inheritance - Is_a? and kind_of? for type checking - Quiz 12easy Inheritance - Protected and private visibility - Quiz 7medium Inheritance - Accessing parent methods - Quiz 10hard Modules and Mixins - Extend for class methods - Quiz 14medium