Ruby beginner question

(Alucard) #1

Hello

I am very new to Ruby and currently install Ruby 1.82.15

My question, what's wrong with the following code?

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

  def to_s
    puts "Song: #{@name}--#{@artist} (#{@duration})"
  end
end
class KaraokeSong < Song
  def initialize(name, artist, duration, lyrics)
    super(name, artist, duration)
    @lyrics = lyrics
  end

  def to_s
    super + " [#{@lyrics}]"
  end
end
aSong = KaraokeSong.new("KSongName", "KSongArtist", 225, "Klyrics")
aSong.to_s

The error is:
Song: KSongName--KSongArtist (225)
song.rbw:33:in `to_s': undefined method `+' for nil:NilClass
(NoMethodError)
  from song.rbw:37

Please help!

Thanks in advance

(Christopher Campbell) #2

Alucard wrote:

Hello

I am very new to Ruby and currently install Ruby 1.82.15

My question, what's wrong with the following code?

New to ruby too, but it looks like you're calling + on "super" and on a string.

Try super.to_s + " [#{@lyrics}]" in to_s

(Joshua Haberman) #3

Omit the "puts": you want to return the string, not print it!

Josh

···

On Aug 20, 2005, at 10:46 AM, Alucard wrote:

Hello

I am very new to Ruby and currently install Ruby 1.82.15

My question, what's wrong with the following code?

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

    def to_s
        puts "Song: #{@name}--#{@artist} (#{@duration})"

(Derek Wyatt) #4

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

  def to_s
    puts "Song: #{@name}--#{@artist} (#{@duration})"
  end
end

This to_s should RETURN a string, not print one.

Definition should be
def to_s
~ "Song: #{@name}--#{@artist} (#{@duration})"
end

class KaraokeSong < Song
  def initialize(name, artist, duration, lyrics)
    super(name, artist, duration)
    @lyrics = lyrics
  end

  def to_s
    super + " [#{@lyrics}]"
  end

Now this will be the return from super (a string) plus a string,
which is what you want. Before, super returned nil

end
aSong = KaraokeSong.new("KSongName", "KSongArtist", 225, "Klyrics")
aSong.to_s

The error is:
Song: KSongName--KSongArtist (225)
song.rbw:33:in `to_s': undefined method `+' for nil:NilClass
(NoMethodError)
  from song.rbw:37

Please help!

Thanks in advance

- --
Derek Wyatt - C++ / Ruby / Unix Programmer
http://rails.derekwyatt.org

(Ara.T.Howard) #5

super doesn't return a string - you've got

   def to_s
     puts "Song: #{@name}--#{@artist} (#{@duration})"
   end

it returns nil (the return value of puts). try

   def to_s
     "Song: #{@name}--#{@artist} (#{@duration})"
   end

hth. and welcome aboard!

-a

···

On Sun, 21 Aug 2005, Alucard wrote:

Hello

I am very new to Ruby and currently install Ruby 1.82.15

My question, what's wrong with the following code?

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

  def to_s
    puts "Song: #{@name}--#{@artist} (#{@duration})"
  end
end
class KaraokeSong < Song
  def initialize(name, artist, duration, lyrics)
    super(name, artist, duration)
    @lyrics = lyrics
  end

  def to_s
    super + " [#{@lyrics}]"
  end
end
aSong = KaraokeSong.new("KSongName", "KSongArtist", 225, "Klyrics")
aSong.to_s

The error is:
Song: KSongName--KSongArtist (225)
song.rbw:33:in `to_s': undefined method `+' for nil:NilClass
(NoMethodError)
  from song.rbw:37

Please help!

Thanks in advance

--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
Your life dwells amoung the causes of death
Like a lamp standing in a strong breeze. --Nagarjuna

===============================================================================

(Alexandru Popescu) #6

#: chris changed the world a bit at a time by saying on 8/20/2005 7:51 PM :#

Alucard wrote:

Hello

I am very new to Ruby and currently install Ruby 1.82.15

My question, what's wrong with the following code?

New to ruby too, but it looks like you're calling + on "super" and on a string.

Try super.to_s + " [#{@lyrics}]" in to_s

I think you should write it:

class Song
  [....]

  def to_s
    "Song: #{@name}--#{@artist} (#{@duration})"
  end
end

class KaraokeSong
  [...]
  def to_s
    super + + " [#{@lyrics}]"
  end

end

[...]
puts aSong.to_s

:alex |.::the_mindstorm::.|

(Alexandru Popescu) #7

#: Alexandru Popescu changed the world a bit at a time by saying on 8/20/2005 8:09 PM :#

#: chris changed the world a bit at a time by saying on 8/20/2005 7:51 PM :#

I think you should write it:

class Song
  [....]

  def to_s
    "Song: #{@name}--#{@artist} (#{@duration})"
  end
end

class KaraokeSong
  [...]
  def to_s
    super + + " [#{@lyrics}]"
  end

end

[...]
puts aSong.to_s

:alex |.::the_mindstorm::.|

definitely no double +, just one +. as it was already explained Song#to_s should return a string, while the initial Song#to_s is just printing one and returning nil.

:alex |.::the_mindstorm::.|

(Alucard) #8

Hi all,

thanks all your reply. It works using the following code. Thanks
again for input and suggestions!

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

  def to_s
    "Song: #{@name}--#{@artist} (#{@duration})"
  end
end

class KaraokeSong < Song
  def initialize(name, artist, duration, lyrics)
    super(name, artist, duration)
    @lyrics = lyrics
  end

  def to_s
    super + " [#{@lyrics}]"
  end
end
aSong = KaraokeSong.new("KSongName", "KSongArtist", 225, "Klyrics")
puts aSong.to_s