[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Calling super with no arguments

Daniel Finnie

3/20/2008 11:50:00 PM

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

4 Answers

Ezra Zygmuntowicz

3/20/2008 11:59:00 PM

0


On Mar 20, 2008, at 4:50 PM, Daniel Finnie 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
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
-- ezra@engineyard.com
-- EngineYard.com


7stud --

3/21/2008 12:06:00 AM

0

Daniel Finnie 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
--
Posted via http://www.ruby-....

Daniel Finnie

3/21/2008 12:15:00 AM

0

Thanks!

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

Dan

On Thu, Mar 20, 2008 at 8:05 PM, 7stud -- <bbxx789_05ss@yahoo.com> wrote:
>
> Daniel Finnie 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
> --
> Posted via http://www.ruby-....
>
>

David A. Black

3/21/2008 1:37:00 AM

0

Hi --

On Fri, 21 Mar 2008, Daniel Finnie 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

--
Upcoming Rails training from David A. Black and Ruby Power and Light:
ADVANCING WITH RAILS, April 14-17 2008, New York City
CORE RAILS, June 24-27 2008, London (Skills Matter)
See http://www.r... for details. Berlin dates coming soon!