Forum: Ruby Stuck on Page 27 of Programming Ruby

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.
2f8b4756de693922efc9e8be6473c933?d=identicon&s=25 Allen Dardenelle (shadow)
on 2005-12-31 21:38
HI all, this is my first post in the forum!  I got the Ruby and Rails
books for Christmas and I am having a lot of fun with them.  But I am
stuck on page 27 in Programming Ruby.  I've gotten the code from the web
site and it's not working.

Here is the code:

!#/usr/local/bin/ruby -w

class Song
  def to_s
    "Song: #@name--#@artist (#@duration)"
  end
end
song = Song.new("Bicylops", "Fleck", 260)
song.to_s

Here is my error:

ruby.rb:8:in `initialize': wrong number of arguments (3 for 0)
(ArgumentError)
        from ruby.rb:8:in `new'
        from ruby.rb:8
B780ee0ee1480454a85df58536702f63?d=identicon&s=25 Alder Green (Guest)
on 2005-12-31 21:48
(Received via mailing list)
Hello Allen,

The problem appears to be with this line:

Song.new("Bicylops", "Fleck", 260)

The error message says "wrong number of arguments (3 for 0)", meaning
that
Song#initialize (the initialize method defined by "def initialize ....
end" inside the Song class) doesn't take any arguments, while in the
above
line you are in fact supplying 3 arguments to it.

So, either the problem is in the call and you're supposed to call
Song.newsans arguments:

Song.new()

Or Song#initialize got ill-defined/overriden in some problematic way.

Option #2 seems more likely; it's not logical that you can create a song
object without even a name.

Regards,
Alder Green
0bee7f89111c8d7551e7c5a39e3c24d7?d=identicon&s=25 simonh (Guest)
on 2005-12-31 21:51
(Received via mailing list)
you need to add the code from earlier in the book. look for:

def initialize(name, artist, duration)

can't remember the exact page i'm afraid.

happy new year
4b174722d1b1a4bbd9672e1ab50c30a9?d=identicon&s=25 Ryan Leavengood (Guest)
on 2005-12-31 21:51
(Received via mailing list)
Hi Allen,

You need to define an initialize method which can take the arguments
you are passing to "new." For example:

class Song
  def initialize(name, artist, duration)
    @name, @artist, @duration = name, artist, duration
  end
end

I'm not sure if that is an oversight in the book, or if maybe the
initialize was introduced earlier and you forgot to include it.

Also you will need a "puts" before song.to_s if you expect to see any
output.

Ryan
191c26cc874ae2a84d1a0d4cb9a33b8c?d=identicon&s=25 Gavri Fernandez (Guest)
on 2005-12-31 21:51
(Received via mailing list)
On 1/1/06, Allen Dardenelle <junk@dardenelle.com> wrote:
>   def to_s
>         from ruby.rb:8:in `new'
>         from ruby.rb:8


This code should be after this:

class Song
def initialize(name, artist, duration)
    @name     = name
    @artist   = artist
    @duration = duration
  end
end

You probably thought that the code you chose to execute was
independent of the code above it. In Ruby, classes are open and new
methods can be added to a class by using the 'class' keyword again and
again :)
2f8b4756de693922efc9e8be6473c933?d=identicon&s=25 Allen Dardenelle (shadow)
on 2005-12-31 22:26
Wow, thanks everyone for the fast replies!

That was the problem alright.

Happy New Year Everyone!!!  :-D
This topic is locked and can not be replied to.