Bird
Raised Fist0
Rubyprogramming~20 mins

Why class-level behavior matters in Ruby - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Class-Level Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Understanding class variables and instance variables
What is the output of this Ruby code?
class Counter
  @@count = 0

  def initialize
    @@count += 1
  end

  def self.count
    @@count
  end
end

c1 = Counter.new
c2 = Counter.new
puts Counter.count
Ruby
class Counter
  @@count = 0

  def initialize
    @@count += 1
  end

  def self.count
    @@count
  end
end

c1 = Counter.new
c2 = Counter.new
puts Counter.count
A2
B0
CRaises an error
D1
Attempts:
2 left
💡 Hint
Class variables are shared across all instances of the class.
❓ Predict Output
intermediate
2:00remaining
Difference between class instance variable and class variable
What will this Ruby code print?
class Example
  @value = 10

  def self.value
    @value
  end

  def value
    @value
  end
end

obj = Example.new
puts Example.value
puts obj.value
Ruby
class Example
  @value = 10

  def self.value
    @value
  end

  def value
    @value
  end
end

obj = Example.new
puts Example.value
puts obj.value
A
nil

10
B
10

nil
C
10

D
nil

nil
Attempts:
2 left
💡 Hint
Class instance variables belong to the class object, not instances.
🔧 Debug
advanced
2:30remaining
Why does this class variable behave unexpectedly?
Consider this Ruby code:
class Parent
  @@var = 1

  def self.var
    @@var
  end
end

class Child < Parent
  @@var = 2
end

puts Parent.var
puts Child.var

What is the output and why?
Ruby
class Parent
  @@var = 1

  def self.var
    @@var
  end
end

class Child < Parent
  @@var = 2
end

puts Parent.var
puts Child.var
ARaises an error due to variable conflict.
B
1
2
Because Child overrides @@var separately.
C
1
1
Because @@var is fixed in Parent.
D
2
2
Because class variables are shared across the inheritance chain.
Attempts:
2 left
💡 Hint
Class variables are shared between parent and child classes.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in class-level method definition
Which option contains a syntax error in defining a class method in Ruby?
A
def method_name.self
  puts 'Hello'
end
B
def ClassName.method_name
  puts 'Hello'
end
C
def self.method_name
  puts 'Hello'
end
D
class &lt;&lt; self
  def method_name
    puts 'Hello'
  end
end
Attempts:
2 left
💡 Hint
Class methods are defined on the class object, not on method names.
🚀 Application
expert
3:00remaining
Predict the output of class instance variable with inheritance
What will this Ruby code print?
class A
  @var = 1

  def self.var
    @var
  end
end

class B < A
  @var = 2
end

puts A.var
puts B.var
Ruby
class A
  @var = 1

  def self.var
    @var
  end
end

class B < A
  @var = 2
end

puts A.var
puts B.var
A
1
1
B
2
2
C
1
2
D
nil
nil
Attempts:
2 left
💡 Hint
Class instance variables are not shared between parent and child classes.

Practice

(1/5)
1. What is the main reason to use class-level behavior in Ruby classes?
easy
A. To share data and methods among all instances of the class
B. To create unique data for each object
C. To make objects run faster
D. To avoid using instance variables

Solution

  1. Step 1: Understand class-level behavior

    Class-level behavior means data or methods shared by all objects of a class, not unique to each object.
  2. Step 2: Identify the purpose

    Sharing data and methods avoids repeating the same information in every object and keeps code clean.
  3. Final Answer:

    To share data and methods among all instances of the class -> Option A
  4. Quick Check:

    Class-level behavior = shared data/methods [OK]
Hint: Class-level means shared by all objects, not individual ones [OK]
Common Mistakes:
  • Confusing class-level with instance-level data
  • Thinking class-level makes objects faster
  • Believing class-level removes instance variables
2. Which of the following is the correct way to define a class method in Ruby?
easy
A. def class.method_name; end
B. def method_name; end
C. def @method_name; end
D. def self.method_name; end

Solution

  1. Step 1: Recall class method syntax

    In Ruby, class methods are defined with def self.method_name inside the class.
  2. Step 2: Check options

    def self.method_name; end uses def self.method_name, which is correct. def method_name; end defines an instance method. def @method_name; end uses invalid syntax. def class.method_name; end is incorrect syntax.
  3. Final Answer:

    def self.method_name; end -> Option D
  4. Quick Check:

    Class method syntax = def self.method_name [OK]
Hint: Class methods start with self. inside class [OK]
Common Mistakes:
  • Defining class methods without self.
  • Using @ for method names
  • Trying def class.method_name syntax
3. What will be the output of this Ruby code?
class Dog
  @@count = 0
  def initialize
    @@count += 1
  end
  def self.count
    @@count
  end
end

Dog.new
Dog.new
puts Dog.count
medium
A. 2
B. 0
C. 1
D. Error

Solution

  1. Step 1: Understand class variable usage

    @@count is a class variable shared by all Dog instances. It starts at 0.
  2. Step 2: Count increments on each new Dog

    Each Dog.new calls initialize, which adds 1 to @@count. Two Dog.new calls increase @@count to 2.
  3. Step 3: Output the class variable

    Dog.count returns @@count, which is 2, so puts prints 2.
  4. Final Answer:

    2 -> Option A
  5. Quick Check:

    Class variable increments twice = 2 [OK]
Hint: Class variables track shared data across all instances [OK]
Common Mistakes:
  • Thinking @@count resets for each object
  • Confusing class variables with instance variables
  • Expecting an error from @@count usage
4. Identify the error in this Ruby class that tries to count instances:
class Cat
  @@count = 0
  def initialize
    @count += 1
  end
  def self.count
    @@count
  end
end

Cat.new
Cat.new
puts Cat.count
medium
A. Missing def self.count method
B. Using @count instead of @@count in initialize
C. Class variable @@count not initialized
D. Syntax error in class definition

Solution

  1. Step 1: Check variable usage in initialize

    The code uses @count (an instance variable) instead of @@count (class variable) to increment count.
  2. Step 2: Understand effect of mistake

    Instance variable @count is nil initially, so @count += 1 causes an error or does not affect class count.
  3. Step 3: Confirm other parts are correct

    Class variable @@count is initialized and count method exists, so no other errors.
  4. Final Answer:

    Using @count instead of @@count in initialize -> Option B
  5. Quick Check:

    Increment class variable with @@count, not @count [OK]
Hint: Class variables start with @@, instance with @ [OK]
Common Mistakes:
  • Incrementing instance variable instead of class variable
  • Assuming @count affects all instances
  • Ignoring variable initialization errors
5. You want to track how many times a method is called across all instances of a class. Which approach correctly implements this in Ruby?
hard
A. Use a global variable $counter incremented inside the instance method
B. Use an instance variable @counter incremented inside the instance method
C. Use a class variable @@counter incremented inside the instance method
D. Use a local variable counter inside the method

Solution

  1. Step 1: Understand the goal

    We want to count method calls shared by all instances, so data must be shared at class level.
  2. Step 2: Evaluate options

    Class variable @@counter is shared by all instances, so incrementing it inside the instance method tracks total calls. Instance variable @counter is unique per object, local variable resets each call, global variable $counter works but is discouraged.
  3. Step 3: Choose best practice

    Using @@counter is the correct and clean way to track calls across all instances.
  4. Final Answer:

    Use a class variable @@counter incremented inside the instance method -> Option C
  5. Quick Check:

    Class variable tracks shared count [OK]
Hint: Use @@ for shared counters, not @ or local variables [OK]
Common Mistakes:
  • Using instance variables which count per object
  • Using local variables which reset each call
  • Using global variables which pollute global scope
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard