Basic Inheritance questions {noob alert:- pickaxe ed. 2, page 27}

J

John Maclean

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?
 
W

Wilson Bilkovich

You can do it one of three ways:
In IRB, require the Song file, and then the KaraokeSong file, in that order=
 
J

John Maclean

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
 
E

Eero Saynatkari

John said:
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

Getting a 'false' in irb means the file is already
loaded (a LoadError would be raised if the file could
not be found); if you want, you can use

load 'song.rb'

This will reload the file each time (seldomly necessary).

As a compatibility-note, you could make your filenames
all lowercase_and_underscored. This will thwart any
case sensitivity issues.

E
 
J

John Maclean

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....
 
E

Eero Saynatkari

John said:
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.
 
J

John Maclean

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...]



John said:
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.
 
M

Mike Stok

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)

... shouldn't you have a call like

super('','','')

here to initialise the parent class variables?
@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...]



John said:
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.

--

Mike Stok <[email protected]>
http://www.stok.co.uk/~mike/

The "`Stok' disclaimers" apply.
 
M

Mike Stok

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)

... shouldn't you have a call like

super('','','')

here to initialise the parent class variables?
@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...]



John said:
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.

--

Mike Stok <[email protected]>
http://www.stok.co.uk/~mike/

The "`Stok' disclaimers" apply.
 
E

Eero Saynatkari

John said:
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...")

There is no #initialize method that takes four parameters
at this point (see below).
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?

All you are getting from the parent class are the actual
variables and an #initialize method that takes three parameters.
In your child class, you define a new #initialize that only takes
one parameter (overriding the one from the parent class) so that
all KaraokeSong.new can see is the one with just one parameter.

Even if the child class had a method looking like this:

def initialize(lyrics)
super
@lyrics = lyrics
end

The problem would still be that the method itself does not
take more than one parameter and you would see an error if
you tried to pass it four. So the correct way indeed is to
have your child class with an #initialize that takes all
four parameters and then call #super with the three parameters
that must go to the parent- (or super-) class.
< Warning messages elided />


E
 
J

John Maclean

Thanks. The child is behaving at last ;)

I think that I'll be grabbing a new book once I find one with slightly
clearer descriptions and code base.

Now I understand that one must declare (if that's the right word), all
of the instance variables in for a child class and any variables that
it will inherit from it's parent. Do the same for the child's method.


John said:
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...")

There is no #initialize method that takes four parameters
at this point (see below).
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?

All you are getting from the parent class are the actual
variables and an #initialize method that takes three parameters.
In your child class, you define a new #initialize that only takes
one parameter (overriding the one from the parent class) so that
all KaraokeSong.new can see is the one with just one parameter.

Even if the child class had a method looking like this:

def initialize(lyrics)
super
@lyrics = lyrics
end

The problem would still be that the method itself does not
take more than one parameter and you would see an error if
you tried to pass it four. So the correct way indeed is to
have your child class with an #initialize that takes all
four parameters and then call #super with the three parameters
that must go to the parent- (or super-) class.
< Warning messages elided />


E
 
T

Timothy Goddard

The pickaxe is as far as I know the best book available on the market.
From a background of occasional fiddling with C++, I personally found
the pickaxe very clear and concise. I have since accomplished at least
ten times as much with ruby as I ever have with any other language.

It sounds like this is simply an issue of needing to experiment and
learn from mistakes to build a better fundamental understanding of
common structures. The best way to learn is by doing, as is true for
all fields. I would just fling yourself in, attempt some of the ruby
quiz challenges and have a good time. Whenever you get stuck or have a
problem, the community here is very friendly and helpful.
 
R

Robert Klemme

2006/1/20 said:
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)

This test is completely superfluous. First, you don't do anything with
the return value and second the next line will throw anyway:
class KaraokeSong < Song
def to_s
super + " [#@lyrics]"
end
end

irb(main):011:0> require 'class_KaraokeSong.rb'
=3D> false

This just indicates that the file has been required already.

Kind regards

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top