Forum: Ruby Newbie - error while trying subclass

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.
28f5e7c5fabbc8aa6f75c4674e017c90?d=identicon&s=25 Rubystudent (Guest)
on 2008-12-14 20:25
(Received via mailing list)
I am trying to subclass in my code (see below). But I am getting the
following error while running the code.

song.rb:12: class/module name must be CONSTANT
class karaokeSong < Song
                   ^
song.rb:23: warning: don't put space before argument parentheses

Not able to figure why "<" is not being accepted for subclassing. I am
using 1.8.6 version on windows. Pls. help

-----------------------------------------------------------------------------------------
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
end

aSong = karaokeSong.new ("Bicyclops", "Fleck", 260, "And, now the")
puts aSong.inspect
puts aSong.to_s
F53b05cdbdf561cfe141f69b421244f3?d=identicon&s=25 David A. Black (Guest)
on 2008-12-14 20:28
(Received via mailing list)
Hi --

On Mon, 15 Dec 2008, Rubystudent wrote:

> I am trying to subclass in my code (see below). But I am getting the
> following error while running the code.
>
> song.rb:12: class/module name must be CONSTANT
> class karaokeSong < Song
>                   ^

karaokeSong isn't a constant. Try KaraokeSong (uppercase 'K').


David
F065301eb65a5d0da8edcb8de9d5e28e?d=identicon&s=25 Tim Greer (Guest)
on 2008-12-14 20:40
(Received via mailing list)
Rubystudent wrote:

>
What version of ruby are you using?

See below for changes:

> class karaokeSong < Song

class KaraokeSong < Song

> aSong = karaokeSong.new ("Bicyclops", "Fleck", 260, "And, now the")

aSong = KaraokeSong.new("Bicyclops", "Fleck", 260, "And, now the")

That should remedy the problem.
28f5e7c5fabbc8aa6f75c4674e017c90?d=identicon&s=25 Rubystudent (Guest)
on 2008-12-14 20:42
(Received via mailing list)
David, Tim - Thanks a lot.
It worked.

Sunil
4df097f56911b410a879c81c176b2d9c?d=identicon&s=25 Mike Cargal (Guest)
on 2008-12-14 21:22
(Received via mailing list)
Try capitalizing karaokeSong

the first error is complaining about the class name not being CONSTANT.
This topic is locked and can not be replied to.