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
On Mar 20, 2008, at 4:50 PM, Daniel F. wrote:
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-
Daniel F. wrote:
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
Thanks!
I really dislike these language features turned methods, I just
assumed that super() would be the same as super
Dan
Hi –
On Fri, 21 Mar 2008, Daniel F. wrote:
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