Constructor overriding

Posted by demas on Stack Overflow See other posts from Stack Overflow or by demas
Published on 2010-04-03T06:11:41Z Indexed on 2010/04/03 6:13 UTC
Read the original article Hit count: 351

Filed under:

I have a library with a class:

class One
  def initialize
    puts "one initialize"
  end
 end

I can not change the declaration and difinition of this class.

I need create new class with my own constructor. Like this:

class Two < One
  def initialize(some)
    puts some
    super
  end
end

one = One.new
one = Two.new("thing")

But when I launch code I got error:

[[email protected]][~/temp]% ruby test.rb 
 one initialize
 thing
 test.rb:10:in `initialize': wrong number of arguments (1 for 0) (ArgumentError)
   from test.rb:15:in `new'
   from test.rb:15:in `<main>'

© Stack Overflow or respective owner

Related posts about ruby