0
0
Rubyprogramming~20 mins

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

Choose your learning style9 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.