Professional Web Applications Themes

[BEGINNER] Usage of 'super' - Ruby

>>>>> "M" == Michael Weller <de> writes: Just write it like this M> class B < A M> def initialize(x, y) M> super super() # call super with no args M> myX = x Guy Decoux...

  1. #1

    Default Re: [BEGINNER] Usage of 'super'

    >>>>> "M" == Michael Weller <de> writes:

    Just write it like this

    M> class B < A
    M> def initialize(x, y)
    M> super

    super() # call super with no args

    M> myX = x


    Guy Decoux





    ts Guest

  2. #2

    Default [BEGINNER] Usage of 'super'

    Hi!
    I have some experience with programming in other languages, but I'm new
    to Ruby.
    Here's a problem I encoutered writing my first program:
    I got these two classes, both classes are defined in the same file
    (let's call it "modA"):

    <code>
    class A
    def initialize
    #inits some instance variables
    end
    end

    class B < A
    def initialize(x, y)
    super
    myX = x
    myY = y
    end
    end
    </code>

    If I create instances of B in the same file (!), everything is OK
    (e.g.:
    b = B.new('sth', 'anotherSth')
    puts b.toS
    ).

    But if create another file and 'include modA' and do the same all I get
    is this:
    ../blogdata.rb:23:in `initialize': wrong number of arguments(0 for 2)
    (ArgumentError)
    from ./blogdata.rb:23:in `new'
    from ./blogdata.rb:23:in `initialize'
    from ./blogdata.rb:32:in `initialize'
    from UI.rbw:5:in `new'
    from UI.rbw:5

    So it seems like B is trying to pass the two params it received on to A.
    A doesn't define 'initialize' with two params to I get an error.
    I looked in the "Ruby Man Docs" and on the syntax page the author states:
    "the super invokes the method which the current method overrides. If no
    arguments given, arguments to the current method passed to the method."

    My questions:
    Can I change this behaviour? Do I need to introduce an "initialize(a,b)'
    in my A class? Why does it work if my code is written in the same file?

    Lots of questions I'm sure somebody here can answer!

    Thanks in advance,
    Michael



    Michael Guest

  3. #3

    Default Re: [BEGINNER] Usage of 'super'

    ts schrieb:
     
    That doesn't do the trick (btw: super is different from super()?).

    Maybe this is important:
    class A
    def initialize
    time = Time.new #here the error happens
    end
    end

    Maybe does A pass the params to Time which doesn't define a no-arg init?

    Michael

    Michael Guest

  4. #4

    Default Re: [BEGINNER] Usage of 'super'


    "Michael Weller" <de> schrieb im Newsbeitrag
    news:bur719$l36i8$news.uni-berlin.de... 
    states: 

    For me it doesn't work in the same file. It should always throw as Guy
    pointed out already. I guess you might have tested something different
    (maybe you forgot "< A" in one of the files).

    Cheers

    robert

    Robert Guest

  5. #5

    Default Re: [BEGINNER] Usage of 'super'

    Hi --

    On Fri, 23 Jan 2004, Michael Weller wrote:
     
    > That doesn't do the trick (btw: super is different from super()?).[/ref]

    super() explicitly says that you want an empty argument list, whereas
    super on its own, as you mentioned, will pass along any arguments
    received by the method in which it appears.


    David

    --
    David A. Black
    net


    David Guest

  6. #6

    Default Re: [BEGINNER] Usage of 'super'

    Robert Klemme schrieb:
     
    Here's my "real" code (BlogData.rb):
    <code>
    class Timed
    attr_reader :time
    def initialize
    time = Time.new # <-- line 23
    end
    end

    class Entry < Timed
    attr_reader :title, :description
    def initialize(titleS, descS)
    super()
    title = titleS
    description = descS
    comments = Array.new
    end
    def to_s
    "E : #{title} -- #{description} [#{time}]"
    end
    end

    class Comment < Timed
    attr_reader :text
    def initialize(commentS)
    super()
    text = commentS
    end
    end

    e = Entry.new('title1','desc1')
    puts e
    </code>

    My shell: 
    E : title1 -- desc1 [Fri Jan 23 15:11:27 Westeuropäische Normalzeit 2004] 

    And here's another file in the directory (t.rb):
    <code>
    require 'BlogData'
    e = Entry.new('title1','desc1')
    puts e
    </code>

    My Shell (with 'puts e' in BlogData.rb deleled): 
    /BlogData.rb:23:in `initialize': wrong number of arguments(0 for 2)
    (ArgumentError)
    from ./BlogData.rb:23:in `new'
    from ./BlogData.rb:23:in `initialize'
    from ./BlogData.rb:32:in `initialize'
    from t.rb:17:in `new'
    from t.rb:17 

    It doesn't work if I "require" my code in another file.

    Writing "def initialize()" or "def initialize" doesn't make a
    difference, does it?

    Cheers!

    Michael




    Michael Guest

  7. #7

    Default Re: [BEGINNER] Usage of 'super'


    Michael Weller <de> writes:
     
    > That doesn't do the trick (btw: super is different from super()?).[/ref]

    I should. I just checked with 1.6.7 and 1.8.0 with:

    (r1.rb)
    class A
    def initialize
    puts 'Class A init'
    time = Time.new #here the error happens
    #inits some instance variables
    end
    end

    class B < A
    def initialize(x, y)
    super()
    myX = x
    myY = y
    end
    end

    (r2.rb)
    require 'r1.rb'

    b = B.new('sth', 'anotherSth')
    puts b.to_s

    (test)
    1024>ruby r2.rb
    Class A init
    #<B:0x40096aac>
    1025>/usr/local/src/ruby-1.6.7/ruby r2.rb
    Class A init
    #<B:0x4009989c>


    Replacing 'super()' with 'super' gives:

    1027>ruby r2.rb
    ../r1.rb:18:in `initialize': wrong number of arguments(2 for 0) (ArgumentError)
    from ./r1.rb:18:in `initialize'
    from r2.rb:10:in `new'
    from r2.rb:10



     

    Nope. Not it.
     

    --
    Daniel Kelley - San Jose, CA
    For email, replace the first dot in the domain with an at.
    Daniel Guest

  8. #8

    Default Re: [BEGINNER] Usage of 'super'

    >>>>> "M" == Michael Weller <de> writes:

    M> Here's my "real" code (BlogData.rb):

    Well, not really :-))

    the line numbers given by ruby are not the same than in your source :-)

    M> require 'BlogData'

    Are you sure that you don't have another file 'BlogData' in your search
    path, try with (in t.rb)

    require './BlogData.rb'

    if it's still give an error, post completely BlogData.rb and t.rb


    Guy Decoux






    ts Guest

  9. #9

    Default Re: [BEGINNER] Usage of 'super'


    "Michael Weller" <de> schrieb im Newsbeitrag
    news:de... 
    > Here's my "real" code (BlogData.rb):
    > <code>
    > class Timed
    > attr_reader :time
    > def initialize
    > time = Time.new # <-- line 23
    > end
    > end
    >
    > class Entry < Timed
    > attr_reader :title, :description
    > def initialize(titleS, descS)
    > super()
    > title = titleS
    > description = descS
    > comments = Array.new
    > end
    > def to_s
    > "E : #{title} -- #{description} [#{time}]"
    > end
    > end
    >
    > class Comment < Timed
    > attr_reader :text
    > def initialize(commentS)
    > super()
    > text = commentS
    > end
    > end
    >
    > e = Entry.new('title1','desc1')
    > puts e
    > </code>
    >
    > My shell: 
    > E : title1 -- desc1 [Fri Jan 23 15:11:27 Westeuropäische Normalzeit[/ref]
    2004] 
    >
    > And here's another file in the directory (t.rb):
    > <code>
    > require 'BlogData'
    > e = Entry.new('title1','desc1')
    > puts e
    > </code>
    >
    > My Shell (with 'puts e' in BlogData.rb deleled): 
    > /BlogData.rb:23:in `initialize': wrong number of arguments(0 for 2)
    > (ArgumentError)
    > from ./BlogData.rb:23:in `new'
    > from ./BlogData.rb:23:in `initialize'
    > from ./BlogData.rb:32:in `initialize'
    > from t.rb:17:in `new'
    > from t.rb:17 [/ref]

    Strange that t.rb is reported to have line 17 while the code above looks
    like only three lines.
     

    And you're 100% sure that the required code is the same as presented
    above? No load path pecularities? Sorry, I can't believe this - it works
    for me:

    15:32:54 [blog]: ruby BlogData.rb
    E : title1 -- desc1 [Fri Jan 23 15:32:59 GMT+1:00 2004]
    15:32:59 [blog]: ruby t.rb
    E : title1 -- desc1 [Fri Jan 23 15:33:03 GMT+1:00 2004]
    E : title1 -- desc1 [Fri Jan 23 15:33:03 GMT+1:00 2004]
    15:33:03 [blog]: cat BlogData.rb
    class Timed
    attr_reader :time
    def initialize
    time = Time.new # <-- line 23
    end
    end

    class Entry < Timed
    attr_reader :title, :description
    def initialize(titleS, descS)
    super()
    title = titleS
    description = descS
    comments = Array.new
    end
    def to_s
    "E : #{title} -- #{description} [#{time}]"
    end
    end

    class Comment < Timed
    attr_reader :text
    def initialize(commentS)
    super()
    text = commentS
    end
    end

    e = Entry.new('title1','desc1')
    puts e
    15:33:09 [blog]: cat t.rb
    require 'BlogData'
    e = Entry.new('title1','desc1')
    puts e
    15:33:13 [blog]:
     

    No difference.

    robert

    Robert Guest

  10. #10

    Default Re: [BEGINNER] Usage of 'super'

    Robert Klemme schrieb:
     
    >>/BlogData.rb:23:in `initialize': wrong number of arguments(0 for 2)
    >>(ArgumentError)
    >> from ./BlogData.rb:23:in `new'
    >> from ./BlogData.rb:23:in `initialize'
    >> from ./BlogData.rb:32:in `initialize'
    >> from t.rb:17:in `new'
    >> from t.rb:17 
    >>
    >>[/ref]
    >
    >Strange that t.rb is reported to have line 17 while the code above looks
    >like only three lines.
    >
    >
    >[/ref]
    I had some commented lines before that statement.
     
    >
    >And you're 100% sure that the required code is the same as presented
    >above? No load path pecularities? Sorry, I can't believe this - it works
    >for me:
    >
    >[/ref]
    "Load path pecularities": like my editor I run the code from (SciTE)? I just tried from my OS's shell and this works.

    Sorry that I made such a big noise!

    Michael


    Michael Guest

  11. #11

    Default Re: [BEGINNER] Usage of 'super'

    ts schrieb:
     [/ref]
    >
    >M> Here's my "real" code (BlogData.rb):
    >
    > Well, not really :-))
    >
    > the line numbers given by ruby are not the same than in your source :-)
    >
    >[/ref]
    Huh, I must find out more about this language ;-)
     

    Yes this does the trick. If I run my code from the editor I need to
    write 'require ./....rb', in the shell 'require ...' is enough.

    Thanks for your help!

    Michael

    Michael Guest

Similar Threads

  1. Replies: 10
    Last Post: December 11th, 12:03 PM
  2. Replies: 2
    Last Post: October 15th, 01:20 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139