[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”):

class A def initialize #inits some instance variables end end

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

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:innew’
from ./blogdata.rb:23:in initialize' from ./blogdata.rb:32:ininitialize’
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

Just write it like this

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

                super() # call super with no args

    @myX = x

Guy Decoux

“Michael Weller” michael@gutschi.de schrieb im Newsbeitrag
news:bur719$l36i8$1@ID-105305.news.uni-berlin.de

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”):

class A def initialize #inits some instance variables end end

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

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?

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

ts schrieb:

Just write it like this

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

           super()  # call super with no args
  @myX = x

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

Robert Klemme schrieb:

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

Here’s my “real” code (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

My shell:

ruby BlogData.rb
E : title1 – desc1 [Fri Jan 23 15:11:27 Westeuropäische Normalzeit 2004]
Exit code: 0

And here’s another file in the directory (t.rb):

require ‘BlogData’
e = Entry.new(‘title1’,‘desc1’)
puts e

My Shell (with ‘puts e’ in BlogData.rb deleled):

ruby t.rb
./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
Exit code: 1

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

Hi –

···

On Fri, 23 Jan 2004, Michael Weller wrote:

ts schrieb:

Just write it like this

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

           super()  # call super with no args
  @myX = x

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

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
dblack@wobblini.net

Michael Weller michael@gutschi.de writes:

ts schrieb:

Just write it like this

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

           super()  # call super with no args
  @myX = x

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

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

Maybe this is important:

Nope. Not it.

···

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


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

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

Well, not really :-))

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

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

“Michael Weller” michael@gutschi.de schrieb im Newsbeitrag
news:40112D72.6010107@gutschi.de

Robert Klemme schrieb:

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

Here’s my “real” code (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

My shell:

ruby BlogData.rb
E : title1 – desc1 [Fri Jan 23 15:11:27 Westeuropäische Normalzeit
2004]
Exit code: 0

And here’s another file in the directory (t.rb):

require ‘BlogData’
e = Entry.new(‘title1’,‘desc1’)
puts e

My Shell (with ‘puts e’ in BlogData.rb deleled):

ruby t.rb
/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
Exit code: 1

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

It doesn’t work if I “require” my code in another file.

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

Writing “def initialize()” or “def initialize” doesn’t make a
difference, does it?

No difference.

robert

Robert Klemme schrieb:

ruby t.rb
/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
Exit code: 1

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

I had some commented lines before that statement.

It doesn’t work if I “require” my code in another file.

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:

“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

ts schrieb:

“M” == Michael Weller michael@gutschi.de writes:

Here’s my “real” code (BlogData.rb):

Well, not really :-))

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

Huh, I must find out more about this language :wink:

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

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