On Dec 1, 2:05 pm, Paul Private <paulus4... / gmail.com> 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 > > Paul > > > > John Joyce wrote: > > On Dec 1, 2007, at 11:58 AM, Todd Benson wrote: > > >>> code mentioned earlier I get a error message like this > > >> names = %w| tiger bear monkey zebra giraffe | > >> zoo = [] > >> names.each { |n| zoo << Animal.new(n) } > > >> zoo.each { |a| puts a.name } > >> puts "\n------------\n" > >> zoo_in_order = zoo.sort_by{ |a| a.name } > >> zoo.each { |a| puts a.name } > > >> Todd > > > Even consider this, simply have class Cursus inherit from another > > class that already implements methods you need such as .sort > > Enumerable or Array might be convenient, but I didn't read all of > > your class Cursus closely... > > In defining your class it is easy to override any inherited method, > > and generally a lot less work to inherit than to create everything > > from nothing. > > > Is het voor en hogeschool in het Nederlands? Polytechnische school? > > -- > Posted viahttp://www.ruby-forum.com/. Like others have mentioned, you're calling <=> on an instance of class Cursus, but haven't defined it for that class. I think what you mean is... cursussen.sort { |a, b| a.to_s <=> b.to_s } ...Cursus#to_s is only implicitly called when a string object is required (like for puts, or when it is coerced by "string +", &c). Otherwise you have to explicitly call it yourself. Regards, Jordan