Ruby - Classes and ObjectsWhich of the following is the correct way to assign an instance variable inside a method?Aname = "Alice"B@name = "Alice"C$name = "Alice"Dname@ = "Alice"Check Answer
Step-by-Step SolutionSolution:Step 1: Identify instance variable assignment syntaxInstance variables start with '@' and are assigned like normal variables.Step 2: Check optionsOnly @name = "Alice" uses '@' correctly before the variable name.Final Answer:@name = "Alice" -> Option BQuick Check:Instance variable assignment = @name = value [OK]Quick Trick: Assign instance variables with @ before the name [OK]Common Mistakes:Omitting @ and creating local variablesUsing $ which is for global variablesPlacing @ after the variable name
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