call parent constructor in ruby

36,980

Solution 1

First, your method should be initialize, not initialization. Then, you can use super to call the parent class method. As for calling C's initializer in A, for clarity, I'd recommend splitting the initialization stuff into a different function, then just calling that function directly. It's easy to implement, and clearer.

Solution 2

Ruby doesn't have constructors, therefore it's obviously not possible to call them, parent or otherwise. Ruby does have methods, however, and in order to call the parent's method with the same name as the currently executing method, you can use the super keyword. [Note: super without arguments is a shortcut for passing the same arguments that were passed into the currently executing method. If you actually want to pass no arguments, you have to do so explicitly: super().]

Solution 3

Use the super method! Ruby does not have multiple inheritance though.

class B

  attr_accessor :b, :bb

  def initialize(b, bb)
    @b, @bb = b, bb
  end

end

module C

end

class A < B
  include C  # <= if C was a class, you'd get: TypeError: wrong argument type Class (expected Module)

  attr_accessor :a, :aa

  def initialize(a,b,aa,bb)
    @a, @aa = a, aa
    super(b, bb)  # <= calls B#initialize
  end

end

a = A.new(1,2,3,4)
puts a.inspect # => #<A:0x42d6d8 @aa=3, @a=1, @b=2, @bb=4>

Solution 4

This code below will print :

A.proc1 B.proc1 C.proc1

module A
  def proc1
    puts "A.proc1"
    super
  end
end

class B
  def proc1
    puts "B.proc1"
  end
end

class C < B
  include A
  def proc1
    super
    puts "C.proc1"
  end
end

C.new.proc1
Share:
36,980
Stan Kurilin
Author by

Stan Kurilin

My full name is Stanislav Vladimirovich Kurilin

Updated on February 07, 2020

Comments

  • Stan Kurilin
    Stan Kurilin over 4 years

    How can I call parents constructor ?

    module C
        attr_accessor :c, :cc
        def initialization c, cc
            @c, @cc = c, cc
        end 
    end
    
    class B
        attr_accessor :b, :bb
        def initialization b, bb
            @b, @bb = b, bb
        end 
    end
    
    
    class A < B
        include C
        attr_accessor :a, :aa
        def initialization (a, b, c, aa, bb, cc)
            #call B::initialization - ?
            #call C::initialization - ?
            @a, @aa = a, aa
        end
    end
    

    Thanks.

    • oligan
      oligan about 13 years
      Is it idiomatic for a module to have an initialize method?
    • Stan Kurilin
      Stan Kurilin about 13 years
      @Andrew Grimm, probably, no. You are right. I was just starting ruby in April 2010 ;)
  • maček
    maček about 14 years
    @Jörg, +1. Thanks for the mention of the shortcut. Overriding methods just became so much more elegant :)
  • oligan
    oligan about 13 years
    With regards to "passing the same arguments": If you do a = 42 before using super without brackets, it'll use the new object referred to by a, not the old one.
  • WojonsTech
    WojonsTech over 11 years
    i know you got the answer right by saying you use super, but for someone that is super new to ruby I just did not know until i looked at lower examples.
  • Leopd
    Leopd about 11 years
    What do you mean by "ruby doesn't have constuctors"?
  • Jörg W Mittag
    Jörg W Mittag about 11 years
    @Leopd: Can you clarify your question? I'm not really sure what's unclear about that statement.
  • Leopd
    Leopd about 11 years
    How does the initialize method differ from a construtor? It's a method that gets called when an object is created -- it quacks like a duck.
  • Jörg W Mittag
    Jörg W Mittag about 11 years
    @Leopd: the initialize method differs from a constructor in that it is a method, not a constructor.
  • 0112
    0112 over 9 years
    Code examples are always more helpful.
  • 0112
    0112 over 9 years
    @JörgWMittag Constructor: Class Method that creates objects. Initialize: Class Method that creates objects. ??? Quack Quack? I still don't see the difference. Is there something in the definition of constructor that I'm missing?
  • Engr. Hasanuzzaman Sumon
    Engr. Hasanuzzaman Sumon about 9 years
    @alex0112 As far as i know new is ruby constructor not initialize. Actually initialize can't create object. it's new that create solid new object and internally call initialize for initialization. Of course always welcome to let me know if i am wrong :)
  • Jörg W Mittag
    Jörg W Mittag about 9 years
    @Engr.HasanuzzamanSumon: If there even were something like a "constructor" in Ruby, then it would be Class#allocate, not Class#new. Class#allocate creates a new object. Class#new is simply a convenience method calls Class#allocate and then #initialize.
  • Jörg W Mittag
    Jörg W Mittag about 9 years
    @alex0112: Yes. You are missing that a constructor is not a method. But all of Class#new, Class#allocate and #initialize are methods. Constructors have special rules about when they are allowed to be called, how they are allowed to be inherited, what they are allowed to contain etc. For example, in Java, constructors must call the parent constructor, and if they don't, that call gets automatically inserted by the compiler.
  • Engr. Hasanuzzaman Sumon
    Engr. Hasanuzzaman Sumon about 9 years
    @JörgWMittag Thank for clear my misconception, but what is actually constructor or does ruby has any constructor ? I am really confused about this.
  • Parker Kemp
    Parker Kemp about 4 years
    "Ruby doesn't have constructors" this is more like a computer science trivia answer than actual useful information... initialize is a constructor for all intents and purposes.
  • Jörg W Mittag
    Jörg W Mittag about 4 years
    @ParkerKemp: In all languages I know of that have constructors (e.g. C++, Java, Scala, C#, Ceylon, Kotlin, D, Eiffel), constructors have three important properties: 1) they construct objects (duh!), 2) they are not associated with any instance (obviously, since they have to construct it first), 3) they are a separate language construct from other callable constructs (e.g. functions, procedures, methods). Even in ECMAScript, where constructors are just functions, they are invoked with a special operator and in that case behave differently from functions. initialize is none of that. It doesn't
  • Jörg W Mittag
    Jörg W Mittag about 4 years
    … automatically transfers to initialize because it is just a method like any other.
  • Parker Kemp
    Parker Kemp about 4 years
    @JörgWMittag I don't disagree with any of that, except that it's important for newcomers to understand. The distinctions you draw, while technically correct, are not going to matter to someone new to ruby, and especially not to someone new to programming in general. As I said, for all intents and purposes initialize can be viewed as a constructor.