John Maclean
1/21/2006 12:11:00 AM
I'm sorry I still don't get it. (To be honest I think that I can narrow
down this problem to the book not having all of the code in a single
place)...
I can understand that for example (p27) is telling us that there's a
"right way" and "wrong way" to specify methods for child instances.. so
what's wrong with this?
#!/usr/bin/env ruby
require 'class_Song.rb'
# test if this class has been loaded correctly
Object.const_defined?(:Song)
class KaraokeSong < Song
# grab all the properties from the parent but add another instance
variable
def initialize(lyrics)
@lyrics = lyrics
end
def to_s
super + " [#@lyrics]"
end
end
song = KaraokeSong.new("And now, the...")
#song = KaraokeSong.new("My Way", "Sinatra", 225, "And now, the...")
song.inspect
song.to_s
puts song
The above has not specified a name, artist and duration - but that's
coming from the parent class, is it not?
jayeola@tp20$ ruby -w class_KaraokeSong.rb
/class_Song.rb:12: warning: instance variable @name not initialized
/class_Song.rb:12: warning: instance variable @artist not initialized
/class_Song.rb:12: warning: instance variable @duration not initialized
/class_Song.rb:12: warning: instance variable @name not initialized
/class_Song.rb:12: warning: instance variable @artist not initialized
/class_Song.rb:12: warning: instance variable @duration not initialized
Song: -- () [And now, the...]
On Sat, 21 Jan 2006 07:37:03 +0900
Eero Saynatkari <ruby-forum-reg@mailinator.com> wrote:
> John Maclean wrote:
> > Thanks for another fast response. Inheritance - I'm going to get
> > this -tonight-!
> >
> > recap;
> > # the parent class
> > cat fx/ruby/chap3/class_Song.rb
> > #!/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
> >
> > # the child class
> >
> > jayeola@tp20$ 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 initialize(name, artist, duration, lyrics)
> > super(name, artist, duration)
> > @lyrics = lyrics
> > end
> >
> > def to_s
> > super + " [#@lyrics]"
> > end
> > end
> >
> > #song = KaraokeSong.new("My Way", "Sinatra", 225, "And now, the...")
> > #song = KaraokeSong.new("Sinatra", 225, "And now, the...")
> > #song.to_s
> > #song.inspect
> >
> >
> > How do we get KaraokeSong to acquire it's parent's attributes
> > -without- hadrcoding "def initialize(name, artist, duration,
> > lyrics)", unless one has to? From the book I get the impression that
> > you can get away with specifying the child's stuff and that it will
> > take properties from it's parent with the keyword super....
>
> If the child needs additional parameters (or otherwise overrides
> the corresponding method in the parent class), you must* define
> the method in the child (super will run the parent's method like
> in your #initialize above). When the parent's behaviour is adequate,
> you do not need to define a method, it is automatically inherited.
>
> An alternative would be to just have an accessor for @lyrics; that
> way you would not need to define #initialize, but you would need to
> call #lyrics= to set the lyrics initially., which on the surface
> would seem the more cumbersome way.
>
>
> E
>
> * Not strictly true; you could circumvent this with some careful
> metaprogramming but it really is not worth it in this situation.
>
--
John Maclean
MSc (DIC)
07739 171 531