Hi --

On Sun, 2 Dec 2007, Paul Private wrote:

> when I do a sort like this it's working correctly
> class Test
> cursussen = ["ruby","php","c","cobalt","java"]
> z=cursussen.sort { |a, b| a <=> b }
> puts z
>
> end
>
> I don't understand why I can't get it to work when I use this
> require "oefeningen/_cursus"
> class Cursus_applic
>  cursussen = [Cursus.new("Ruby - 1","Jan", 18.15, 10),
>  Cursus.new("Ruby - 2","Piet", 18.15, 8),
>  Cursus.new("Java - 1","Els", 14, 15),
>  Cursus.new("Java - 2","Jan", 14, 10),
>  Cursus.new("Java - 3","Piet", 18.15, 8)
>  ]
>
>
>
>
>  puts '5. Alle Java-cursussen, daarna alle andere cursussen: '
>
>  Java, other = cursussen.partition {|cursus| cursus.naam?("Java") }
>  puts Java, other
>
>  puts '6. Alle cursussen gesorteerd op cursus naam: '
> z=cursussen.sort { |a, b| a <=> b }
> puts z
> end
> can you or someone help me out
> thanks for your help

When you do:

   a <=> b

you are actually calling a method on a, with b as argument:

   a.<=>(b)

If a doesn't have a <=> method, then that's an error. You have to
define a <=> method for the Cursus class. Typically, that method would
delegate the comparison to some property of the objects:

   def <=>(other)
     self.name <=> other.name
   end

or something like that. Then comparing two courses would be
accomplished by comparing their names.


David

-- 
Upcoming training by David A. Black/Ruby Power and Light, LLC:
    * Intro to Rails, London, UK, December 3-6 (by Skills Matter)
See http://www.rubypal.com for details and 2008 announcements!