List Info

Thread: Rubyesque way to do multiple <=>




Rubyesque way to do multiple <=>
user name
2006-04-13 19:50:02
Hello All.


I have class with some fields

class Human
  attr_accessor :sex, :age, :second_name, :first_name
end

now I want to sort objects of this class. I've defined
operator <=>

class Human
  def <=>(other)
    [sex, age, second_name, first_name] <=> [sex, age,
second_name,
first_name]
  end
end

Looks good, yeah? But! If I use third-party complex
comparator for some of
the fields, I receive something like:

def <=> (other)
  res = ComplexCustomComparator.compare(sex, other.sex)
  return res if res != 0  
  
  #if they have the same sex, do other comparisons
end

Foooooo!

How can I do the former in more elegant way?

Thanks.

Victor.


[1]

about | contact  Other archives ( Real Estate discussion Medical topics )