[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

question about iterator

Paul Private

12/1/2007 12:52:00 PM

dear
with the below mentioned script I would like to produce first all of the
Java courses and then the Ruby ones.
however I can't seem to get it to work
I know I can use the partition method but I'm not able to get it to
work.
can you please help me out here



require "collecties/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. First all Java courses then followed by the others: '
cursus_java = cursussen.partition {|cursus|cursus_java?(true)}
puts '6. Alle cursussen gesorteerd op cursus naam: '
puts '7. Alle cursussen voorafgegaan met de index: '

end
and the cursus.rb is mentioned here below
class Cursus
attr :naam, false
attr_reader :docent
attr :tijdstip, false
attr_reader :aantal_cursisten
def initialize naam, docent, tijdstip, aantal
@naam = naam
@docent = docent
@tijdstip = tijdstip
@aantal_cursisten = aantal
end

def overdag?
@tijdstip < 18
end

def naam? cursus_naam
start = @naam.slice(0, cursus_naam.length)
start == cursus_naam
end

def to_s
tijdstip = overdag? ? "overdag" : "\'s avonds"
"\tDe cursus \'#{@naam}\' wordt #{tijdstip} gegeven door #{@docent}"
end
end


thanks for your help
--
Posted via http://www.ruby-....

14 Answers

David A. Black

12/1/2007 2:42:00 PM

0

Hi --

On Sat, 1 Dec 2007, Paul Private wrote:

> dear
> with the below mentioned script I would like to produce first all of the
> Java courses and then the Ruby ones.
> however I can't seem to get it to work
> I know I can use the partition method but I'm not able to get it to
> work.
> can you please help me out here
>
>
>
> require "collecties/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. First all Java courses then followed by the others: '
> cursus_java = cursussen.partition {|cursus|cursus_java?(true)}

You're using a non-existent method, "cursus_java?" You probably want
to do:

java, non_java = cursussen.partition {|cursus| cursus.naam?("Java") }

or something along those lines.

> puts '6. Alle cursussen gesorteerd op cursus naam: '
> puts '7. Alle cursussen voorafgegaan met de index: '
>
> end
> and the cursus.rb is mentioned here below
> class Cursus
> attr :naam, false
> attr_reader :docent
> attr :tijdstip, false
> attr_reader :aantal_cursisten

I believe that attr + false is the same as attr_reader -- except more
cryptic :-) It's best to stick to:

attr_reader
attr_writer
attr_accessor

since the true/false parameter is not self-explanatory.


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.r... for details and 2008 announcements!

Paul Private

12/1/2007 4:09:00 PM

0

david thanks for your help
I have another question here perhaps you can help me out here
normally when you do a sort it's working like this

cursus =["java", "cobalt","php","ruby"]
x=cursus.sort
puts x

this is working like a charm but when I try to implement this in the
code mentioned earlier I get a error message like this
oefeningen/_cursus_applic.rb:20:in `sort': undefined method `<=>' for
#<Cursus:0x28b55f4> (NoMethodError)
from oefeningen/_cursus_applic.rb:20

how can I solve this one?
thanks for your help

Paul
David A. Black wrote:
> Hi --
>
> On Sat, 1 Dec 2007, Paul Private wrote:
>
>> require "collecties/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. First all Java courses then followed by the others: '
>> cursus_java = cursussen.partition {|cursus|cursus_java?(true)}
>
> You're using a non-existent method, "cursus_java?" You probably want
> to do:
>
> java, non_java = cursussen.partition {|cursus| cursus.naam?("Java") }
>
> or something along those lines.
>
>> puts '6. Alle cursussen gesorteerd op cursus naam: '
>> puts '7. Alle cursussen voorafgegaan met de index: '
>>
>> end
>> and the cursus.rb is mentioned here below
>> class Cursus
>> attr :naam, false
>> attr_reader :docent
>> attr :tijdstip, false
>> attr_reader :aantal_cursisten
>
> I believe that attr + false is the same as attr_reader -- except more
> cryptic :-) It's best to stick to:
>
> attr_reader
> attr_writer
> attr_accessor
>
> since the true/false parameter is not self-explanatory.
>
>
> David

--
Posted via http://www.ruby-....

Robert Klemme

12/1/2007 4:12:00 PM

0

On 01.12.2007 13:51, Paul Private wrote:
> dear
> with the below mentioned script I would like to produce first all of the
> Java courses and then the Ruby ones.
> however I can't seem to get it to work
> I know I can use the partition method but I'm not able to get it to
> work.
> can you please help me out here

I would not use partition but a custom order. It might also be helpful
if you introduce an attribute for the course category. You can then use
that for ordering. And it works with more than two categories as well -
which you cannot say from the standard partition method.

Kind regards

robert

Todd Benson

12/1/2007 5:58:00 PM

0

On Dec 1, 2007 10:08 AM, Paul Private <paulus4605@gmail.com> wrote:
> david thanks for your help
> I have another question here perhaps you can help me out here
> normally when you do a sort it's working like this
>
> cursus =["java", "cobalt","php","ruby"]
> x=cursus.sort
> puts x
>
> this is working like a charm but when I try to implement this in the
> code mentioned earlier I get a error message like this
> oefeningen/_cursus_applic.rb:20:in `sort': undefined method `<=>' for
> #<Cursus:0x28b55f4> (NoMethodError)
> from oefeningen/_cursus_applic.rb:20
>
> how can I solve this one?
> thanks for your help

Ruby doesn't know how to compare Cursus objects. You must tell it how
by defining a <=> method, or you can use the #sort_by method with
something that understands <=> (like a string).

If you are simply sorting by alphabetical order of one attribute, then
here's a simple example...

class Animal
attr_reader :name
def initialize name
@type = name
end
end

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

John Joyce

12/1/2007 6:21:00 PM

0


On Dec 1, 2007, at 11:58 AM, Todd Benson wrote:

> On Dec 1, 2007 10:08 AM, Paul Private <paulus4605@gmail.com> wrote:
>> david thanks for your help
>> I have another question here perhaps you can help me out here
>> normally when you do a sort it's working like this
>>
>> cursus =["java", "cobalt","php","ruby"]
>> x=cursus.sort
>> puts x
>>
>> this is working like a charm but when I try to implement this in the
>> code mentioned earlier I get a error message like this
>> oefeningen/_cursus_applic.rb:20:in `sort': undefined method `<=>' for
>> #<Cursus:0x28b55f4> (NoMethodError)
>> from oefeningen/_cursus_applic.rb:20
>>
>> how can I solve this one?
>> thanks for your help
>
> Ruby doesn't know how to compare Cursus objects. You must tell it how
> by defining a <=> method, or you can use the #sort_by method with
> something that understands <=> (like a string).
>
> If you are simply sorting by alphabetical order of one attribute, then
> here's a simple example...
>
> class Animal
> attr_reader :name
> def initialize name
> @type = name
> end
> end
>
> 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?

Paul Private

12/1/2007 8:06:00 PM

0

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 via http://www.ruby-....

David A. Black

12/1/2007 8:15:00 PM

0

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.r... for details and 2008 announcements!

MonkeeSage

12/1/2007 11:01:00 PM

0

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-....

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

Paul Private

12/2/2007 6:20:00 AM

0

THanks Jordan
this was exactly what I was looking for

Paul

Jordan Callicoat wrote:
> On Dec 1, 2:05 pm, Paul Private <paulus4...@gmail.com> wrote:
>> class Cursus_applic
>> puts Java, other
>>
>> >> zoo.each { |a| puts a.name }
>> > 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-....
>
> 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

--
Posted via http://www.ruby-....

David A. Black

12/2/2007 7:36:00 AM

0

Hi --

On Sun, 2 Dec 2007, John Joyce wrote:

>
> On Dec 1, 2007, at 11:58 AM, Todd Benson wrote:
>
>> On Dec 1, 2007 10:08 AM, Paul Private <paulus4605@gmail.com> wrote:
>>> david thanks for your help
>>> I have another question here perhaps you can help me out here
>>> normally when you do a sort it's working like this
>>>
>>> cursus =["java", "cobalt","php","ruby"]
>>> x=cursus.sort
>>> puts x
>>>
>>> this is working like a charm but when I try to implement this in the
>>> code mentioned earlier I get a error message like this
>>> oefeningen/_cursus_applic.rb:20:in `sort': undefined method `<=>' for
>>> #<Cursus:0x28b55f4> (NoMethodError)
>>> from oefeningen/_cursus_applic.rb:20
>>>
>>> how can I solve this one?
>>> thanks for your help
>>
>> Ruby doesn't know how to compare Cursus objects. You must tell it how
>> by defining a <=> method, or you can use the #sort_by method with
>> something that understands <=> (like a string).
>>
>> If you are simply sorting by alphabetical order of one attribute, then
>> here's a simple example...
>>
>> class Animal
>> attr_reader :name
>> def initialize name
>> @type = name
>> end
>> end
>>
>> 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.

The problem, though, is that you don't need Cursus objects to know
about sort; you need them to know about <=>. The object that needs to
know about sort is the collection you're sorting, which is (in all
likelihood) already an array. The objects inside the collection need
to have the <=> method.


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.r... for details and 2008 announcements!