I want to be able to compare objects with fixnums.
class Dog
include Comparable
def <=>(other)
1
end
end
Given the class above why this works:
dog = Dog.new
array = [2, 1, 4, dog]
array.min
=> 1
But this is not so:
dog = Dog.new
array = [dog, 2, 1, 4]
array.min
ArgumentError: comparison of Fixnum with Dog failed
from (irb):11:in `each'
from (irb):11:in `min'
from (irb):11
When the object is the first element, why can't I compare the object with numbers? Is there any way to solve this problem? I want the Dog object to always be the highest value when it is compared with any other value in the array, so I set it to 1 in the <=> method.
Thank!
source
share