Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby class object

Ruby class object

Intro of ruby class data structure

Oursky Limited

May 03, 2012
Tweet

More Decks by Oursky Limited

Other Decks in Programming

Transcript

  1. Revisit OO what is classes? It is just an ADT(Abstract

    Data T ypes) http://www.youtube.com/watch? v=GDVAHA0oyJU Thursday, 3 May, 12
  2. Method Lookup 1. follow receiver’s klass pointer the search the

    m_tbl 2. follow super pointer and search the m_tbl 3. Repeat 1,2 4. invoke method_missing on original receiver, and go though 1-3 again. Thursday, 3 May, 12
  3. module A def hello :helloA end end module B include

    A def hello :helloB end end module C include A def hello :helloC end end class D include C include B end D.new.hello Thursday, 3 May, 12
  4. D super B B klass objectD klass C C super

    klass A A A super klass klass super Modules Thursday, 3 May, 12
  5. @ @@ Both @ and @@ are value stored at

    `iv_tbl` instance_variable_get class_variable_get class A @var = :classinst def initialize @var = :inst @@var = :class end def ivar @var end def cvar @@var end end a = A.new a.ivar a.cvar a.var A.instance_variable_get(:@var) a.instance_variable_get(:@var) a.instance_variable_get(:@@var) a.class_variable_get(:@var) a.class_variable_get(:@@var) http://rhg.rubyforge.org/chapter06.html Thursday, 3 May, 12
  6. @@ often have real use @@ Will dependent on the

    inheritance hierarchy class A @@x = 3 end class B < A @@x = 4 end A.class_variable_get(:@@x) Thursday, 3 May, 12