Help solving problem in ruby

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

···

--
Posted via http://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.

···

On Dec 9, 8:54 pm, Govinda Khanal <khana...@gmail.com> wrote:

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
--
Posted viahttp://www.ruby-forum.com/.

Govinda Khanal wrote:

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

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

thanx for your help i solve the problem ...

what actually is to_s and when we use this one?

···

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

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

···

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

Govinda Khanal 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.

Govinda Khanal 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)

···

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