From: Wilson Bilkovich Date: 2006-01-21T04:28:25+09:00 Subject: Re: Basic Inheritance questions {noob alert:- pickaxe ed. 2, page 27} You can do it one of three ways: In IRB, require the Song file, and then the KaraokeSong file, in that order. Edit the KaraokeSong file to require the Song before defining the child class. Put both of the class definitions in a single file, with the parent first, and then the child. In order for KaraokeSong < Song to work, this method needs to return true: Object.const_defined?(:Song) That will be the case with each of the three possibilities above. Good luck, and have fun, --Wilson. On 1/20/06, John Maclean wrote: > Chaps, > > Say you have a class Song. You'd make a file for that class with the > following; > > #!/usr/bin/env ruby > class Song > def initialize(name, artist, duration) > # define instance variables > @name = name > @artist = artist > @duration = duration > end > > def to_s > # method for displaying these instancess > "Song: #@name--#@artist (#@duration)" > end > end > > # test the class by creating a new object > #song = Song.new("Bicylops", "Fleck", 260) > #song.inspect > > Now it's time to make a "child" class.... > > jayeola@tp20$ cat class_KaraokeSong.rb > #!/usr/bin/env ruby > class KaraokeSong < Song > def initialize(name, artist, duration, lyrics) > super(name, artist, duration) > @lyrics = lyrics > end > > # def to_s > # # method for displaying these instancess > # "Song: #@name--#@artist (#@duration)" > # end > end > > When I created the parent it was simple to `irb` and then `require > class_Song.rb`. > > How would one use this child class? Not quite clear from the Pickaxe > book. Do I create a new file for KaraokeSong as I have done or have > both the "super" and "child" in the same file? > > -- > John Maclean > MSc (DIC) > 07739 171 531 > > >