Challenge - 5 Problems
Ruby Comparable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of comparing two objects with Comparable
What is the output of this Ruby code using the Comparable module?
Ruby
class Box include Comparable attr_reader :volume def initialize(w, h, d) @volume = w * h * d end def <=>(other) volume <=> other.volume end end box1 = Box.new(2, 3, 4) box2 = Box.new(3, 2, 4) puts box1 == box2 puts box1 < box2 puts box1 > box2
Attempts:
2 left
💡 Hint
Remember that Comparable uses the <=> method to compare objects.
✗ Incorrect
Both boxes have volume 24 (2*3*4 and 3*2*4). So box1 == box2 is true. Neither is less or greater than the other, so false for < and >.
❓ Predict Output
intermediate2:00remaining
Sorting objects with Comparable
What will be the output of this Ruby code that sorts objects using Comparable?
Ruby
class Person include Comparable attr_reader :age def initialize(age) @age = age end def <=>(other) age <=> other.age end def to_s "Age: #{age}" end end people = [Person.new(30), Person.new(20), Person.new(25)] sorted = people.sort sorted.each { |p| puts p }
Attempts:
2 left
💡 Hint
The sort method uses the <=> operator defined in Comparable.
✗ Incorrect
The <=> compares ages, so sorting arranges people by increasing age: 20, 25, 30.
❓ Predict Output
advanced2:00remaining
Custom comparison with multiple attributes
What is the output of this Ruby code that compares objects by two attributes using Comparable?
Ruby
class Product include Comparable attr_reader :price, :rating def initialize(price, rating) @price = price @rating = rating end def <=>(other) comp = price <=> other.price comp.zero? ? rating <=> other.rating : comp end end p1 = Product.new(100, 4.5) p2 = Product.new(100, 4.7) p3 = Product.new(90, 5.0) puts p1 > p2 puts p3 < p1 puts p2 == p2
Attempts:
2 left
💡 Hint
First compare price, then rating if prices are equal.
✗ Incorrect
p1 and p2 have same price, but p1 rating 4.5 < 4.7 so p1 > p2 is false. p3 price 90 < 100 so p3 < p1 true. p2 == p2 true.
❓ Predict Output
advanced2:00remaining
Effect of missing <=> method in Comparable
What error or output does this Ruby code produce when Comparable is included but <=> is missing?
Ruby
class Item include Comparable attr_reader :value def initialize(value) @value = value end end item1 = Item.new(10) item2 = Item.new(20) puts item1 < item2
Attempts:
2 left
💡 Hint
Comparable requires <=> method to work.
✗ Incorrect
Including Comparable without defining <=> causes NoMethodError when comparison operators are used.
🧠 Conceptual
expert2:00remaining
Determining number of items after sorting with Comparable
Given this Ruby code, how many items will be in the sorted array?
Ruby
class Score include Comparable attr_reader :points def initialize(points) @points = points end def <=>(other) points <=> other.points end end scores = [Score.new(10), Score.new(20), Score.new(10), Score.new(30)] sorted_scores = scores.sort.uniq { |s| s.points } puts sorted_scores.size
Attempts:
2 left
💡 Hint
uniq with a block removes duplicates based on the block's return value.
✗ Incorrect
There are 4 scores but two have points 10. uniq { |s| s.points } removes duplicates by points, leaving 3 unique points: 10, 20, 30.