------ art_21719_20359869.1144960601139 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline On 4/13/06, Victor Shepelev <vshepelev / imho.com.ua> wrote: > > 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. > > > Actually you just use your first version of method <=> without worrying about the second. Run the following code and you will clearly see why ;) ------------------8<------------------------------- class A def <=>(other) puts "<=> in A" 0 end end puts "If at first you do not succeed" [1, A.new] <=> [2, A.new] puts "..." [A.new ] <=> [A.new] ----------------------->8---------------------------- Ruby takes care of everything for you Cheers Robert -- Deux choses sont infinies : l'univers et la bóŐise humaine ; en ce qui concerne l'univers, je n'en ai pas acquis la certitude absolue. - Albert Einstein ------ art_21719_20359869.1144960601139--