0
0
Rubyprogramming~20 mins

Comparable module usage in Ruby - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ruby Comparable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
A
true
true
false
B
false
true
false
C
false
false
true
D
true
false
false
Attempts:
2 left
💡 Hint
Remember that Comparable uses the <=> method to compare objects.
Predict Output
intermediate
2: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 }
A
Age: 30
Age: 25
Age: 20
B
Age: 20
Age: 25
Age: 30
C
Age: 25
Age: 20
Age: 30
D
Age: 20
Age: 30
Age: 25
Attempts:
2 left
💡 Hint
The sort method uses the <=> operator defined in Comparable.
Predict Output
advanced
2: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
A
false
true
true
B
true
false
true
C
false
false
false
D
true
true
false
Attempts:
2 left
💡 Hint
First compare price, then rating if prices are equal.
Predict Output
advanced
2: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
ANoMethodError: undefined method `<=>` for #<Item:...>
Bfalse
Ctrue
DSyntaxError: unexpected end-of-input
Attempts:
2 left
💡 Hint
Comparable requires <=> method to work.
🧠 Conceptual
expert
2: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
A2
B4
C3
D1
Attempts:
2 left
💡 Hint
uniq with a block removes duplicates based on the block's return value.