Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > question about iterator

Reply
Thread Tools

question about iterator

 
 
Paul Private
Guest
Posts: n/a
 
      12-01-2007
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, ,
Cursus.new('Java - 1','Els', 14, 15),
Cursus.new('Java - 2','Jan', 14, 10),
Cursus.new('Java - 3','Piet', 18.15,
]

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-forum.com/.

 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      12-01-2007
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, ,
> Cursus.new('Java - 1','Els', 14, 15),
> Cursus.new('Java - 2','Jan', 14, 10),
> Cursus.new('Java - 3','Piet', 18.15,
> ]
>
> 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.rubypal.com for details and 2008 announcements!

 
Reply With Quote
 
 
 
 
Paul Private
Guest
Posts: n/a
 
      12-01-2007
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, ,
>> Cursus.new('Java - 1','Els', 14, 15),
>> Cursus.new('Java - 2','Jan', 14, 10),
>> Cursus.new('Java - 3','Piet', 18.15,
>> ]
>>
>> 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-forum.com/.

 
Reply With Quote
 
Todd Benson
Guest
Posts: n/a
 
      12-01-2007
On Dec 1, 2007 10:08 AM, Paul Private <(E-Mail Removed)> 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

 
Reply With Quote
 
John Joyce
Guest
Posts: n/a
 
      12-01-2007

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

> On Dec 1, 2007 10:08 AM, Paul Private <(E-Mail Removed)> 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?

 
Reply With Quote
 
Paul Private
Guest
Posts: n/a
 
      12-01-2007
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, ,
Cursus.new("Java - 1","Els", 14, 15),
Cursus.new("Java - 2","Jan", 14, 10),
Cursus.new("Java - 3","Piet", 18.15,
]




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-forum.com/.

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      12-01-2007
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, ,
> Cursus.new("Java - 1","Els", 14, 15),
> Cursus.new("Java - 2","Jan", 14, 10),
> Cursus.new("Java - 3","Piet", 18.15,
> ]
>
>
>
>
> 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!

 
Reply With Quote
 
MonkeeSage
Guest
Posts: n/a
 
      12-01-2007
On Dec 1, 2:05 pm, Paul Private <(E-Mail Removed)> 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, ,
> Cursus.new("Java - 1","Els", 14, 15),
> Cursus.new("Java - 2","Jan", 14, 10),
> Cursus.new("Java - 3","Piet", 18.15,
> ]
>
> 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
 
Reply With Quote
 
Paul Private
Guest
Posts: n/a
 
      12-02-2007
THanks Jordan
this was exactly what I was looking for

Paul

Jordan Callicoat wrote:
> On Dec 1, 2:05 pm, Paul Private <(E-Mail Removed)> 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-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


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

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      12-02-2007
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 <(E-Mail Removed)> 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.rubypal.com for details and 2008 announcements!

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
List iterator assignment fails, assert iterator not dereferencable David Bilsby C++ 5 10-09-2007 02:05 PM
What makes an iterator an iterator? Steven D'Aprano Python 28 04-20-2007 03:34 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to convert from std::list<T*>::iterator to std::list<const T*>::iterator? PengYu.UT@gmail.com C++ 6 10-30-2005 03:31 AM
Iterator doubts, Decision on Iterator usage greg C++ 6 07-17-2003 01:26 PM



Advertisments