Comparing an object with fixnum

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!

+4
source share
3 answers

For comparison with numeric types, you can implement coerce:

class Dog
  include Comparable

  def initialize(age)
    @age = age
  end

  def <=>(other)
    @age <=> other
  end

  def coerce(numeric)
    [numeric, @age]
  end
end

Dog.new(5) <=> Dog.new(5)  #=>  0
Dog.new(5) <=> 1           #=>  1
         1 <=> Dog.new(5)  #=> -1

[1, 3, Dog.new(2)].sort
#=> 1, #<Dog @age=2>, 3]

The above sorting can be achieved without implementation <=>and coercewith sort_by:

class Dog
  attr_accessor :age

  def initialize(age)
    @age = age
  end
end

[1, 3, Dog.new(2)].sort_by { |obj| obj.is_a?(Dog) ? obj.age : obj }
#=> 1, #<Dog @age=2>, 3]

There's also min_by, and max_by.

+4
source

Dog Fixnum, Fixnum:

class Fixnum
  alias :my_compare :'<=>'
  def <=>(other)
    if other.is_a? Dog
      return -1
    end
    my_compare(other)
  end
end

array.min
# => 1

array.max
# => #<Dog:0x8f712fc>
+3

This is because it is <=>not a binary relation in the mathematical sense (i.e., defined on A ^ 2 for some set A), but it is a method on the object and is not symmetrical. Allowing otherto Foo#<=>(other)be an instance Bardoes not mean that otherto Bar#<=>(other)be Foo.

0
source

Source: https://habr.com/ru/post/1548094/


All Articles