Challenge - 5 Problems
Ruby Class Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple Ruby class method call
What is the output of this Ruby code?
Ruby
class Greeter def greet "Hello, friend!" end end g = Greeter.new puts g.greet
Attempts:
2 left
💡 Hint
Look at what the greet method returns and what puts does.
✗ Incorrect
The greet method returns the string "Hello, friend!" and puts prints it to the screen.
❓ Predict Output
intermediate2:00remaining
Output when accessing an instance variable
What will this Ruby code print?
Ruby
class Person def initialize(name) @name = name end def name @name end end p = Person.new("Alice") puts p.name
Attempts:
2 left
💡 Hint
The initialize method sets @name, and the name method returns it.
✗ Incorrect
The instance variable @name is set to "Alice" and returned by the name method, so puts prints "Alice".
🔧 Debug
advanced2:00remaining
Identify the error in this class declaration
What error does this Ruby code produce?
Ruby
class Animal def speak puts "Roar" end def speak puts "Meow" end end cat = Animal.new cat.speak
Attempts:
2 left
💡 Hint
Check if all methods and classes are properly closed with 'end'.
✗ Incorrect
The second speak method is missing an 'end', causing a syntax error due to incomplete method definition.
❓ Predict Output
advanced2:00remaining
Output of class variable usage
What is the output of this Ruby code?
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
Attempts:
2 left
💡 Hint
Class variables are shared among all instances.
✗ Incorrect
Each new instance increments @@count by 1. After two instances, @@count is 2.
🧠 Conceptual
expert2:00remaining
Understanding inheritance and method overriding
Given these Ruby classes, what will be printed when calling d.speak?
Ruby
class Dog def speak "Woof" end end class LoudDog < Dog def speak super.upcase end end d = LoudDog.new puts d.speak
Attempts:
2 left
💡 Hint
The 'super' keyword calls the parent method, then upcase changes the string.
✗ Incorrect
LoudDog's speak calls Dog's speak with super, which returns "Woof", then upcase converts it to "WOOF".