# Help solving problem in ruby

**URL:** <https://www.ruby-forum.com/t/help-solving-problem-in-ruby/180038>\
**Category:** Ruby\
**Created:** [December 10, 2009, 2:54am UTC](https://www.ruby-forum.com/t/help-solving-problem-in-ruby/180038 "2009-12-10T02:54:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kgovinda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c68b51/32.png) [@kgovinda](https://www.ruby-forum.com/u/kgovinda)\
**Post date:** [December 10, 2009, 2:54am UTC](https://www.ruby-forum.com/t/help-solving-problem-in-ruby/180038/1 "2009-12-10T02:54:09Z")

</div>

i am a beginner and having problem in linux with this code:

class Song  
def to\_s  
“Song: #@name–#@artist (#@duration)”  
end  
end  
song = Song.new(“Bicylops”, “Fleck”, “2.60”)  
song.to\_s

=\> the output looks like this:

a.rb:6:in `initialize': wrong number of arguments (3 for 0) (ArgumentError) from a.rb:6:in`new’  
from a.rb:6

---

<div class="post-metadata">

**Author:** ![kgovinda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c68b51/32.png) [@kgovinda](https://www.ruby-forum.com/u/kgovinda)\
**Post date:** [December 10, 2009, 3:16am UTC](https://www.ruby-forum.com/t/help-solving-problem-in-ruby/180038/2 "2009-12-10T03:16:03Z")

</div>

On Dec 9, 8:54 pm, Govinda K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> the output looks like this:
> 
> ## a.rb:6:in `initialize': wrong number of arguments (3 for 0) (ArgumentError) from a.rb:6:in `new’ from a.rb:6
> 
> Posted viahttp://www.ruby-forum.com/.

In Ruby one can reopen a class at anytime and “monkey patch” it to add  
new methods etc. Looks like you skipped the part in Programming Ruby  
where the Song class was initially defined.

---

<div class="post-metadata">

**Author:** ![kgovinda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c68b51/32.png) [@kgovinda](https://www.ruby-forum.com/u/kgovinda)\
**Post date:** [December 10, 2009, 3:16am UTC](https://www.ruby-forum.com/t/help-solving-problem-in-ruby/180038/3 "2009-12-10T03:16:39Z")

</div>

Govinda K. wrote:

> =\> the output looks like this:
> 
> a.rb:6:in `initialize': wrong number of arguments (3 for 0) (ArgumentError) from a.rb:6:in`new’  
> from a.rb:6

If you looked earlier in the chapter you would have seen an initialize  
method that needs to be part of the Song class.

---

<div class="post-metadata">

**Author:** ![kgovinda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c68b51/32.png) [@kgovinda](https://www.ruby-forum.com/u/kgovinda)\
**Post date:** [December 10, 2009, 3:21am UTC](https://www.ruby-forum.com/t/help-solving-problem-in-ruby/180038/4 "2009-12-10T03:21:36Z")

</div>

thanx for your help i solve the problem …

what actually is to\_s and when we use this one?

---

<div class="post-metadata">

**Author:** ![kgovinda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c68b51/32.png) [@kgovinda](https://www.ruby-forum.com/u/kgovinda)\
**Post date:** [December 10, 2009, 3:22am UTC](https://www.ruby-forum.com/t/help-solving-problem-in-ruby/180038/5 "2009-12-10T03:22:36Z")

</div>

yes, i skipped the first part. thanx for your help…

---

<div class="post-metadata">

**Author:** ![kgovinda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c68b51/32.png) [@kgovinda](https://www.ruby-forum.com/u/kgovinda)\
**Post date:** [December 10, 2009, 5:00am UTC](https://www.ruby-forum.com/t/help-solving-problem-in-ruby/180038/6 "2009-12-10T05:00:50Z")

</div>

Govinda K. wrote:

> thanx for your help i solve the problem …
> 
> what actually is to\_s and when we use this one?

to\_s converts an object to a string. In this case it converts the song  
object to a format more readable than just using puts. It is also used  
to convert numbers to strings, etc.

---

<div class="post-metadata">

**Author:** ![kgovinda](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/c68b51/32.png) [@kgovinda](https://www.ruby-forum.com/u/kgovinda)\
**Post date:** [December 10, 2009, 8:07am UTC](https://www.ruby-forum.com/t/help-solving-problem-in-ruby/180038/7 "2009-12-10T08:07:55Z")

</div>

Govinda K. wrote:

> yes, i skipped the first part. thanx for your help…

For the sake of other beginners, added initialize and a puts to last  
line. Note, last line could be simplified to ‘puts song’.

class Song  
def initialize(name, artist, duration)  
@name, @artist, @duration = name, artist, duration  
end  
def to\_s  
“Song: #@name–#@artist (#@duration)”  
end  
end

song = Song.new(“Bicylops”, “Fleck”, “2.60”)

puts song.to\_s # =\> Song: Bicylops–Fleck (2.60)
