Forum: Ruby Module's superclass?

9d8fd118fbbda12178880ff5d353b785?d=identicon&s=25 Eugeni Kurtov (lessless)
on 2013-03-21 15:27
===
module S1
  def initialize
    puts 's1'
  end
end
module T1
  def initialize
    puts 't1'
  end
end

class MyC
  include S1
  include T1
  def initialize
    puts 'myc'
    super()
  end
end

MyC.new
====

output
====
myc
t1
====

so, I can't call all the chain of initialize() of all included modules
in MyC ?

and

====
module S1
  def initialize
    puts 's1'
  end
end
module T1
  def initialize
    puts 't1'
    super()
  end
end

class MyC
  include S1
  include T1
  def initialize
    puts 'myc'
    super()
  end
end

MyC.new
====

output
====
myc
t1
s1
====
how's that super() in T1 calls initialize() in S1 ???
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse mailing lists (Guest)
on 2013-03-22 00:04
(Received via mailing list)
On Thu, Mar 21, 2013 at 9:27 AM, Eugeni Kurtov <lists@ruby-forum.com>
wrote:
> end
> MyC.new
>
>     puts 't1'
>   end
> ====
> how's that super() in T1 calls initialize() in S1 ???
>
> --
> Posted via http://www.ruby-forum.com/.
>

Look at MyC.ancestors:
 => [MyC, T1, S1, Object, Kernel, BasicObject]

so the super() in MyC#initialize calls T1#initialize. Then super() in
T1#initialize will call S1#initialize. If you call super() in
S1#initialize, it would call Object#initialize, and so on up the
ancestor chain.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.