Challenge - 5 Problems
Instance Variable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Ruby code using instance variables?
Consider the following Ruby class and code. What will be printed when this code runs?
Ruby
class Dog def initialize(name) @name = name end def speak "Woof! My name is #{@name}." end end fido = Dog.new("Fido") puts fido.speak
Attempts:
2 left
💡 Hint
Instance variables start with @ and belong to the object instance.
✗ Incorrect
The instance variable @name is set in initialize and used in speak. It holds the value 'Fido', so the output includes the name.
❓ Predict Output
intermediate2:00remaining
What is the value of @count after running this Ruby code?
Look at this Ruby class that uses an instance variable @count. What will be the value of @count for the object after these calls?
Ruby
class Counter def initialize @count = 0 end def increment @count += 1 end def current @count end end c = Counter.new c.increment c.increment c.current
Attempts:
2 left
💡 Hint
Each call to increment adds 1 to @count.
✗ Incorrect
The @count starts at 0, then increment is called twice, adding 1 each time, so @count becomes 2.
🔧 Debug
advanced2:00remaining
Why does this Ruby code not print the name when accessing @name?
This Ruby code tries to print @name but does not print the name. Why? What is the output?
Ruby
class Person def initialize(name) name = name end def greet "Hello, my name is #{@name}." end end p = Person.new("Alice") puts p.greet
Attempts:
2 left
💡 Hint
Check how the instance variable @name is assigned in initialize.
✗ Incorrect
The code assigns local variable 'name' to itself, not to @name. So @name is never set and is nil. Interpolating nil in a string produces an empty string.
🧠 Conceptual
advanced2:00remaining
What happens to instance variables when you create multiple objects?
If you create two objects from the same Ruby class, each with different values for an instance variable @value, what will happen when you access @value on each object?
Attempts:
2 left
💡 Hint
Think about how instance variables belong to each object instance.
✗ Incorrect
Instance variables belong to each object separately. Each object keeps its own copy of @value, so they do not affect each other.
❓ Predict Output
expert2:00remaining
What is the output of this Ruby code using instance variables and inheritance?
Analyze this Ruby code with inheritance and instance variables. What will be printed?
Ruby
class Animal def initialize(name) @name = name end def speak "I am an animal named #{@name}." end end class Cat < Animal def speak super + " Meow!" end end kitty = Cat.new("Whiskers") puts kitty.speak
Attempts:
2 left
💡 Hint
The Cat class calls super to use the Animal's speak method and adds its own text.
✗ Incorrect
The Cat class inherits from Animal. The speak method calls super, which returns 'I am an animal named Whiskers.', then adds ' Meow!'. So the full output is 'I am an animal named Whiskers. Meow!'.