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

Discussion in 'Ruby' started by John Maclean, Jan 20, 2006.

  1. John Maclean

    John Maclean Guest

    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, Jan 20, 2006
    #1
    1. Advertising

  2. You can do it one of three ways:
    In IRB, require the Song file, and then the KaraokeSong file, in that order=
     
    Wilson Bilkovich, Jan 20, 2006
    #2
    1. Advertising

  3. John Maclean

    John Maclean Guest

    Re: Basic Inheritance questions {noob alert:- pickaxe ed. 2, page27}

    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
     
    John Maclean, Jan 20, 2006
    #3
  4. Re: Basic Inheritance questions {noob alert:- pickaxe ed. 2,

    John Maclean wrote:
    > 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


    --
    Posted via http://www.ruby-forum.com/.
     
    Eero Saynatkari, Jan 20, 2006
    #4
  5. John Maclean

    John Maclean Guest

    Re: Basic Inheritance questions {noob alert:- pickaxe ed. 2,

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


    --
    John Maclean
    MSc (DIC)
    07739 171 531
     
    John Maclean, Jan 20, 2006
    #5
  6. Re: Basic Inheritance questions {noob alert:- pickaxe ed. 2,

    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.

    --
    Posted via http://www.ruby-forum.com/.
     
    Eero Saynatkari, Jan 20, 2006
    #6
  7. John Maclean

    John Maclean Guest

    Re: Basic Inheritance questions {noob alert:- pickaxe ed. 2,

    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 <> 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
     
    John Maclean, Jan 21, 2006
    #7
  8. John Maclean

    Mike Stok Guest

    Re: Basic Inheritance questions {noob alert:- pickaxe ed. 2,

    On 20-Jan-06, at 7:11 PM, John Maclean wrote:

    > 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...]
    >
    >
    >
    > On Sat, 21 Jan 2006 07:37:03 +0900
    > Eero Saynatkari <> 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
    >
    >
    >


    --

    Mike Stok <>
    http://www.stok.co.uk/~mike/

    The "`Stok' disclaimers" apply.
     
    Mike Stok, Jan 21, 2006
    #8
  9. John Maclean

    Mike Stok Guest

    Re: Basic Inheritance questions {noob alert:- pickaxe ed. 2,

    On 20-Jan-06, at 7:11 PM, John Maclean wrote:

    > 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...]
    >
    >
    >
    > On Sat, 21 Jan 2006 07:37:03 +0900
    > Eero Saynatkari <> 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
    >
    >
    >


    --

    Mike Stok <>
    http://www.stok.co.uk/~mike/

    The "`Stok' disclaimers" apply.
     
    Mike Stok, Jan 21, 2006
    #9
  10. Re: Basic Inheritance questions {noob alert:- pickaxe ed. 2,

    John Maclean wrote:
    > 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

    --
    Posted via http://www.ruby-forum.com/.
     
    Eero Saynatkari, Jan 21, 2006
    #10
  11. John Maclean

    John Maclean Guest

    Re: Basic Inheritance questions {noob alert:- pickaxe ed. 2,

    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.


    On Sat, 21 Jan 2006 10:00:04 +0900 Eero
    Saynatkari <> wrote:

    > John Maclean wrote:
    > > 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
    >



    --
    John Maclean
    MSc (DIC)
    07739 171 531
     
    John Maclean, Jan 21, 2006
    #11
  12. Re: Basic Inheritance questions {noob alert:- pickaxe ed. 2,

    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.
     
    Timothy Goddard, Jan 21, 2006
    #12
  13. 2006/1/20, 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)


    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

    --
    Have a look: http://www.flickr.com/photos/fussel-foto/
     
    Robert Klemme, Jan 21, 2006
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bouke Woudstra

    (noob alert) why doesn't this work?

    Bouke Woudstra, Mar 22, 2005, in forum: Python
    Replies:
    1
    Views:
    266
    Diez B. Roggisch
    Mar 22, 2005
  2. Bouke Woudstra

    Re: (noob alert) why doesn't this work?

    Bouke Woudstra, Mar 22, 2005, in forum: Python
    Replies:
    2
    Views:
    284
    Bengt Richter
    Mar 23, 2005
  3. John Maclean

    Files and variables (noob alert)

    John Maclean, Jan 27, 2006, in forum: Ruby
    Replies:
    3
    Views:
    109
    John Maclean
    Jan 27, 2006
  4. Krekna Mektek
    Replies:
    4
    Views:
    127
    Giles Bowkett
    Dec 30, 2006
  5. simplicity

    Basic questions about alert() and confirm()

    simplicity, Nov 28, 2007, in forum: Javascript
    Replies:
    6
    Views:
    94
    Thomas 'PointedEars' Lahn
    Nov 29, 2007
Loading...

Share This Page