Challenge - 5 Problems
Ruby Object 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?
Consider this Ruby code snippet. What will it print when run?
Ruby
puts 5.class puts 'hello'.class puts nil.class
Attempts:
2 left
💡 Hint
Check the class of each value using the .class method.
✗ Incorrect
In Ruby, every value is an object. The .class method shows the class of the object. Numbers are Integer, text is String, and nil is an object of class NilClass.
🧠 Conceptual
intermediate1:30remaining
Why is everything an object in Ruby?
Which of the following best explains why everything in Ruby is an object?
Attempts:
2 left
💡 Hint
Think about how Ruby allows you to call methods on any value.
✗ Incorrect
Ruby is designed so that every value is an object. This means all data types are instances of classes, which lets you call methods on any value in a consistent way.
🔧 Debug
advanced1:30remaining
What error does this Ruby code raise?
What error will this Ruby code produce when run?
Ruby
5.upcaseAttempts:
2 left
💡 Hint
Check if the Integer class has the method 'upcase'.
✗ Incorrect
The method upcase is defined for String objects, not for Integer. Calling it on an integer raises a NoMethodError.
❓ Predict Output
advanced2:00remaining
What is the output of this Ruby code involving objects?
What will this Ruby code print?
Ruby
class Animal def speak 'Hello' end end obj = Animal.new puts obj.speak
Attempts:
2 left
💡 Hint
Look at the method defined inside the class and what is called on the object.
✗ Incorrect
The Animal class has a method speak that returns 'Hello'. Calling obj.speak prints 'Hello'.
🧠 Conceptual
expert2:30remaining
How does Ruby's object model affect method calls on literals?
Why can you call methods like
.to_s or .class on literals such as numbers or strings in Ruby?Attempts:
2 left
💡 Hint
Think about what it means for everything to be an object in Ruby.
✗ Incorrect
In Ruby, literals like numbers and strings are objects. This means they have methods defined on their classes, so you can call methods like .to_s or .class directly on them.