Forum: Ruby Problem with example from pickax book

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
49e37d55fb2fa0b6c3c7286f9ad45be6?d=identicon&s=25 Ben Edwards (Guest)
on 2007-07-13 16:52
(Received via mailing list)
Working through the Pickax Ruby programing book and cant get the
following example 'compile':-

irb(main):040:0* class SongList
irb(main):041:1>   MAX_TIME = 5*60 # 5 minutes
irb(main):042:1>
irb(main):043:1*   def Songlist.is_too_long(song)
irb(main):044:2>     return song.duration > MAX_TIME
irb(main):045:2>   end
irb(main):046:1> end
(irb):41: warning: already initialized constant MAX_TIME
NameError: uninitialized constant SongList::Songlist
        from (irb):43
        from :0

Any idea why?

Ben
D0338c0de4cb3c5c17300396159933d1?d=identicon&s=25 Axel Etzold (Guest)
on 2007-07-13 17:21
(Received via mailing list)
Dear Ben,

> irb(main):040:0* class SongList
> irb(main):041:1>   MAX_TIME = 5*60 # 5 minutes
> irb(main):042:1>
> irb(main):043:1*   def Songlist.is_too_long(song)
                         ^^^^^^^^^

Just leave out the underlined "Songlist."
Best regards,

Axel
E0ed615bd6632dd23165e045e3c1df09?d=identicon&s=25 Florian Gross (Guest)
on 2007-07-13 17:30
(Received via mailing list)
On Jul 13, 4:51 pm, "Ben Edwards" <funkyt...@gmail.com> wrote:
> (irb):41: warning: already initialized constant MAX_TIME
> NameError: uninitialized constant SongList::Songlist

Songlist needs to be SongList. Or just change it to "self". :)
49e37d55fb2fa0b6c3c7286f9ad45be6?d=identicon&s=25 Ben Edwards (Guest)
on 2007-07-13 17:31
(Received via mailing list)
On 13/07/07, Axel Etzold <AEtzold@gmx.de> wrote:
> Dear Ben,
>
> > irb(main):040:0* class SongList
> > irb(main):041:1>   MAX_TIME = 5*60 # 5 minutes
> > irb(main):042:1>
> > irb(main):043:1*   def Songlist.is_too_long(song)
>                          ^^^^^^^^^
>
> Just leave out the underlined "Songlist."

Its actually supposed to be a class method, but ive spotted the error.
 It should be SongList, not Songlist (the L needs to be upper case).

Ta,
Ben
This topic is locked and can not be replied to.