Calling super with no arguments

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

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

···

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

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-forum.com/\.

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-forum.com/\.

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.rubypal.com for details. Berlin dates coming soon!