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