Calling super with no arguments

D

Daniel Finnie

Hi all,

Does anyone know how to invoke super so that it passes no arguments to
the superclass' method?

For example:

class A
def initialize
puts "making an A"
end
end

class B < A
def initialize var
puts "making a #{var} b"
super
end
end

B.new "great"

results in:
making a great b
/tmp/super.rb:10:in `initialize': wrong number of arguments (1 for 0)
(ArgumentError)
from /tmp/super.rb:10:in `initialize'
from /tmp/super.rb:14:in `new'
from /tmp/super.rb:14

Because calling super with no arguments causes the method's arguments
to be repeated. How do I stop this? Temporarily, I stuck a *args on
class A's initialize method but that is not pretty!

Dan
 
E

Ezra Zygmuntowicz

Hi all,

Does anyone know how to invoke super so that it passes no arguments to
the superclass' method?

For example:

class A
def initialize
puts "making an A"
end
end

class B < A
def initialize var
puts "making a #{var} b"
super
super()


When you call super as a bare word, it passes the args from the
enclosing method automatically, to force it to pass no args you need
to use super() .


Cheers-

- Ezra Zygmuntowicz
-- Founder & Software Architect
-- (e-mail address removed)
-- EngineYard.com
 
7

7stud --

Daniel said:
Hi all,

Does anyone know how to invoke super so that it passes no arguments to
the superclass' method?

For example:

class A
def initialize
puts "making an A"
end
end

class B < A
def initialize var
puts "making a #{var} b"
super
end
end

B.new "great"

results in:
making a great b
/tmp/super.rb:10:in `initialize': wrong number of arguments (1 for 0)
(ArgumentError)
from /tmp/super.rb:10:in `initialize'
from /tmp/super.rb:14:in `new'
from /tmp/super.rb:14

class A
def initialize
puts "making an A"
end
end

class B < A
def initialize var
puts "making a #{var} b"
super()
end
end

B.new "great"

--output:--
making a great b
making an A
 
D

Daniel Finnie

Thanks!

I really dislike these language features turned methods, I just
assumed that super() would be the same as super

Dan
 
D

David A. Black

Hi --

Thanks!

I really dislike these language features turned methods, I just
assumed that super() would be the same as super

I understand what you mean, though I think it pays to think of super
as a keyword that finds a method, rather than a method itself. The
idea of super, without the (), is sort of like: Do what we're
currently doing all over again, but using the next highest definition.
So it's a kind of recapitulation, arguments and all.


David
 

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
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top