[BEGINNER] Usage of 'super'

T

ts

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
 
M

Michael Weller

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
 
M

Michael Weller

ts said:
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
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
 
R

Robert Klemme

Michael Weller said:
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?

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
 
M

Michael Weller

Robert said:
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):
<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:
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):
<code>
require 'BlogData'
e = Entry.new('title1','desc1')
puts e
</code>

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
 
D

Daniel Kelley

Michael Weller said:
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.
 
T

ts

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
 
R

Robert Klemme

Michael Weller said:
Robert said:
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):
<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:
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):
<code>
require 'BlogData'
e = Entry.new('title1','desc1')
puts e
</code>

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
 
M

Michael Weller

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

Michael Weller

ts said:
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 :)
Huh, I must find out more about this language ;-)
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

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
 

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
474,141
Messages
2,570,818
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top