# Stuck on Page 27 of Programming Ruby

**URL:** <https://www.ruby-forum.com/t/stuck-on-page-27-of-programming-ruby/51467>\
**Category:** Ruby\
**Created:** [December 31, 2005, 9:38pm UTC](https://www.ruby-forum.com/t/stuck-on-page-27-of-programming-ruby/51467 "2005-12-31T21:38:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![shadow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@shadow](https://www.ruby-forum.com/u/shadow)\
**Post date:** [December 31, 2005, 9:38pm UTC](https://www.ruby-forum.com/t/stuck-on-page-27-of-programming-ruby/51467/1 "2005-12-31T21:38:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shadow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@shadow](https://www.ruby-forum.com/u/shadow)\
**Post date:** [December 31, 2005, 9:48pm UTC](https://www.ruby-forum.com/t/stuck-on-page-27-of-programming-ruby/51467/2 "2005-12-31T21:48:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shadow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@shadow](https://www.ruby-forum.com/u/shadow)\
**Post date:** [December 31, 2005, 9:51pm UTC](https://www.ruby-forum.com/t/stuck-on-page-27-of-programming-ruby/51467/3 "2005-12-31T21:51:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shadow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@shadow](https://www.ruby-forum.com/u/shadow)\
**Post date:** [December 31, 2005, 9:51pm UTC](https://www.ruby-forum.com/t/stuck-on-page-27-of-programming-ruby/51467/4 "2005-12-31T21:51:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![shadow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@shadow](https://www.ruby-forum.com/u/shadow)\
**Post date:** [December 31, 2005, 10:26pm UTC](https://www.ruby-forum.com/t/stuck-on-page-27-of-programming-ruby/51467/5 "2005-12-31T22:26:45Z")

</div>

Wow, thanks everyone for the fast replies!

That was the problem alright.

Happy New Year Everyone!!! 😃

---

<div class="post-metadata">

**Author:** ![shadow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5fc32e/32.png) [@shadow](https://www.ruby-forum.com/u/shadow)\
**Post date:** [December 31, 2005, 9:51pm UTC](https://www.ruby-forum.com/t/stuck-on-page-27-of-programming-ruby/51467/6 "2005-12-31T21:51:07Z")

</div>

On 1/1/06, Allen D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 🙂
