From: John Maclean Date: 2006-01-21T06:37:54+09:00 Subject: Re: Basic Inheritance questions {noob alert:- pickaxe ed. 2, page 27} Thanks for that speedy reply. I've decided to require the within the child. One thing to note is that when I do require the child irb shows the following.. cat fx/ruby/chap3/class_KaraokeSong.rb #!/usr/bin/env ruby require 'class_Song.rb' # test if this class has been loaded correctly Object.const_defined?(:Song) class KaraokeSong < Song def to_s super + " [#@lyrics]" end end irb(main):011:0> require 'class_KaraokeSong.rb' => false On Sat, 21 Jan 2006 04:28:25 +0900 Wilson Bilkovich wrote: > 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 > > > > > > > > > -- John Maclean MSc (DIC) 07739 171 531