Recall & Review
beginner
What is a class variable in Ruby?A class variable in Ruby is a variable shared among a class and all its subclasses. It is prefixed with @@ and holds data common to the class hierarchy.Click to reveal answer
beginner
How do you declare a class variable in Ruby?You declare a class variable by prefixing the variable name with @@ inside a class, for example: @@count = 0Click to reveal answer
intermediate
Why can class variables (@@) be dangerous in Ruby?Class variables are dangerous because they are shared across the entire class hierarchy, so changes in one subclass affect others unexpectedly, leading to bugs that are hard to track.Click to reveal answer
intermediate
What is a safer alternative to class variables in Ruby?Using class instance variables (e.g., @count inside class << self or self methods) is safer because they belong only to the specific class, not shared with subclasses.Click to reveal answer
advanced
Explain with an example how class variables can cause unexpected behavior.If a superclass and its subclass both modify @@var, the changes affect each other. For example, if superclass sets @@count = 1 and subclass sets @@count = 5, both see the last value 5, which can cause confusion.Click to reveal answer
What symbol is used to declare a class variable in Ruby?
✗ Incorrect
Class variables in Ruby always start with @@.
What is a main risk of using class variables (@@) in Ruby?
✗ Incorrect
Class variables are shared across the class hierarchy, so changes in one subclass affect others.
Which is a safer alternative to class variables for storing class-specific data?
✗ Incorrect
Class instance variables belong only to the class itself, not shared with subclasses.
If a superclass and subclass both modify @@var, what happens?
✗ Incorrect
Class variables are shared across the class hierarchy, so both classes see the same variable.
How do you access a class instance variable inside a class method?
✗ Incorrect
Class instance variables use a single @ and can be accessed inside class methods.
Describe what class variables (@@) are in Ruby and why they can cause problems in inheritance.
Think about how one change affects all related classes.
You got /4 concepts.
Explain the difference between class variables (@@) and class instance variables (@) in Ruby.
Focus on scope and sharing of data.
You got /4 concepts.