metawhat?

1,180 views
1,123 views

Published on

A look into Ruby's object model, metaclasses, and method dispatch.

Published in: Technology, Sports
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,180
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
18
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • you care because you want to know how your tools actually work

  • how many have seen this?
    how many have written this?
    not for class methods?
    cout?
  • this is what I first used class << self for
    we want class methods

  • objects just instance variables + pointer to a class
    no methods

  • objects just instance variables + pointer to a class
    no methods

  • notice there is no methods/behavior
    by process of elimination, methods are stored on the class

  • class is an object with behavior and a superclass
  • class is an object with behavior and a superclass
  • class is an object with behavior and a superclass
  • class is an object with behavior and a superclass
  • class is an object with behavior and a superclass
  • class is an object with behavior and a superclass

  • how do we call methods on foo?
  • how do we call methods on foo?
  • Gradually build up object model by using method dispatch as a guide
    eventually call method_missing
  • Gradually build up object model by using method dispatch as a guide
    eventually call method_missing
  • Gradually build up object model by using method dispatch as a guide
    eventually call method_missing
  • Gradually build up object model by using method dispatch as a guide
    eventually call method_missing








  • bob’s class is primate, primate is a sublass of Object
  • bob’s class is primate, primate is a sublass of Object
  • bob’s class is primate, primate is a sublass of Object
  • bob’s class is primate, primate is a sublass of Object
  • bob’s class is primate, primate is a sublass of Object
  • bob’s class is primate, primate is a sublass of Object
  • bob’s class is primate, primate is a sublass of Object
  • bob’s class is primate, primate is a sublass of Object
  • instance-specific behavior










  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • singleton classes define instance-specific behavior
  • many different names
    slight differences in meaning sometimes
    singleton class stores instance-specific behavior
    created automatically on request (class << obj)
    classes are objects
    metaclasses store instance specific behavior for classes
  • ruby magic to hide the singleton
    groovy has explicit access to singleton

  • remember when we defined instance specific methods on joe

  • instance-specific behavior is class methods
    dereference class pointer, look up method, climb inheritance chain
    class of Primate is class b/c we did Class.new
  • instance-specific behavior is class methods
    dereference class pointer, look up method, climb inheritance chain
    class of Primate is class b/c we did Class.new
  • instance-specific behavior is class methods
    dereference class pointer, look up method, climb inheritance chain
    class of Primate is class b/c we did Class.new
  • instance-specific behavior is class methods
    dereference class pointer, look up method, climb inheritance chain
    class of Primate is class b/c we did Class.new
  • instance-specific behavior is class methods
    dereference class pointer, look up method, climb inheritance chain
    class of Primate is class b/c we did Class.new
  • instance-specific behavior is class methods
    dereference class pointer, look up method, climb inheritance chain
    class of Primate is class b/c we did Class.new
  • instance-specific behavior is class methods
    dereference class pointer, look up method, climb inheritance chain
    class of Primate is class b/c we did Class.new
  • instance-specific behavior is class methods
    dereference class pointer, look up method, climb inheritance chain
    class of Primate is class b/c we did Class.new
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • Missing Primate’s superclass (Object)
  • we have part of ruby’s object model, lets continue by fixing a problem

  • ruby creates singletons on-demand, but metaclasses automatically
    that includes object
    insert object into graph
  • added object’s metaclass
    object’s class methods will not be visible on primate
    Primate.inspect
  • added object’s metaclass
    object’s class methods will not be visible on primate
    Primate.inspect
  • added object’s metaclass
    object’s class methods will not be visible on primate
    Primate.inspect
  • added object’s metaclass
    object’s class methods will not be visible on primate
    Primate.inspect
  • added object’s metaclass
    object’s class methods will not be visible on primate
    Primate.inspect
  • added object’s metaclass
    object’s class methods will not be visible on primate
    Primate.inspect
  • added object’s metaclass
    object’s class methods will not be visible on primate
    Primate.inspect

  • Class has a metaclass too, but it’s pretty much limited to new
    Primate.foo inherits from Object
    the metaclass of my superclass is the superclass of my metaclass
  • Class has a metaclass too, but it’s pretty much limited to new
    Primate.foo inherits from Object
    the metaclass of my superclass is the superclass of my metaclass
  • Class has a metaclass too, but it’s pretty much limited to new
    Primate.foo inherits from Object
    the metaclass of my superclass is the superclass of my metaclass
  • Class has a metaclass too, but it’s pretty much limited to new
    Primate.foo inherits from Object
    the metaclass of my superclass is the superclass of my metaclass
  • Class has a metaclass too, but it’s pretty much limited to new
    Primate.foo inherits from Object
    the metaclass of my superclass is the superclass of my metaclass
  • Class has a metaclass too, but it’s pretty much limited to new
    Primate.foo inherits from Object
    the metaclass of my superclass is the superclass of my metaclass
  • Class has a metaclass too, but it’s pretty much limited to new
    Primate.foo inherits from Object
    the metaclass of my superclass is the superclass of my metaclass
  • Class has a metaclass too, but it’s pretty much limited to new
    Primate.foo inherits from Object
    the metaclass of my superclass is the superclass of my metaclass
  • what about module?

  • ruby trickery with joe’ superclass
    look at the diagram, what is Joe’s singleton’s superclass?
    including a module inserts it into the inheritance chain
  • mention method_missing
  • ruby hides it, but we can get it out
  • ruby 1.8 only and does not effect method dispatch
  • iclass code redacted
    ruby 1.8 only



  • you can take singleton_class

  • whenever you def object.method, you define a method on the singleton of that object

  • class << just switches into the context of the singleton class of whatever object you specify
  • << = “give me ur singleton class”
    switches into the context of the singleton class of self









  • metawhat?

    1. 1. metawhat? a look into the mysterious metaclass and Ruby’s object model Brandon Dimcheff @bdimcheff Ruby Midwest 2010
    2. 2. class << self
    3. 3. class Foo class << self def bar "hello" end end end Foo.bar # => "hello"
    4. 4. class Foo class << self def bar "hello" end end end Foo.bar # => "hello"
    5. 5. what’s an object?
    6. 6. struct RObject { struct RBasic basic; struct st_table *iv_tbl; }; ruby.h
    7. 7. struct RObject { struct RBasic basic; struct st_table *iv_tbl; }; struct RBasic { unsigned long flags; VALUE klass; }; ruby.h
    8. 8. struct RObject { struct RBasic basic; struct st_table *iv_tbl; }; struct RBasic { unsigned long flags; VALUE klass; }; ruby.h
    9. 9. what is an object? foo class ivar hash
    10. 10. objects don’t have methods
    11. 11. struct RClass { struct RBasic basic; struct st_table *iv_tbl; struct st_table *m_tbl; VALUE super; }; ruby.h
    12. 12. RObject struct RClass { struct RBasic basic; struct st_table *iv_tbl; struct st_table *m_tbl; VALUE super; }; ruby.h
    13. 13. struct RClass { struct RBasic basic; struct st_table *iv_tbl; struct st_table *m_tbl; VALUE super; methods }; ruby.h
    14. 14. struct RClass { struct RBasic basic; struct st_table *iv_tbl; struct st_table *m_tbl; VALUE super; methods }; superclass pointer ruby.h
    15. 15. what is a class? super Bar class ivar hash methods
    16. 16. super Bar class ivar hash foo class ivar hash methods
    17. 17. super Object Bar class ivar hash foo class ivar hash methods
    18. 18. method dispatch
    19. 19. method dispatch 1. Follow my class pointer
    20. 20. method dispatch 1. Follow my class pointer 2. Check the methods table for my method
    21. 21. method dispatch 1. Follow my class pointer 2. Check the methods table for my method 3. Call the method, or follow the super pointer if the method doesn’t exist
    22. 22. method dispatch 1. Follow my class pointer 2. Check the methods table for my method 3. Call the method, or follow the super pointer if the method doesn’t exist 4. Repeat 2-3 if the method was not found
    23. 23. method dispatch super Bar class ivar hash foo class ivar hash methods
    24. 24. method dispatch super 1. Follow the class pointer Bar class ivar hash foo class ivar hash methods
    25. 25. method dispatch super 1. Follow the class pointer Bar class ivar hash foo class ivar hash methods 2. Check the method table
    26. 26. method dispatch super 3. Follow the super pointer 1. Follow the class pointer Bar class ivar hash foo class ivar hash methods 2. Check the method table
    27. 27. class Primate def opposable_thumbs true end end bob = Primate.new bob.opposable_thumbs # => true
    28. 28. Bob
    29. 29. Object super class bob Primate
    30. 30. Object bob.opposable_thumbs super class bob Primate
    31. 31. Object bob.opposable_thumbs super class bob Primate
    32. 32. Object bob.opposable_thumbs super class bob Primate me tho ds
    33. 33. Object bob.opposable_thumbs super class bob Primate me tho ds #opposable_thumbs
    34. 34. Object bob.opposable_thumbs super class bob Primate me tho ds #opposable_thumbs # => true
    35. 35. joe = Primate.new def joe.opposable_thumbs false end joe.opposable_thumbs # => false bob.opposable_thumbs # => true
    36. 36. Joe
    37. 37. Pretend this is missing Joe
    38. 38. Object super class joe Primate
    39. 39. Object joe.opposable_thumbs super class joe Primate
    40. 40. Object joe.opposable_thumbs super class joe Primate
    41. 41. Object joe.opposable_thumbs super class joe Primate me tho ds
    42. 42. Object joe.opposable_thumbs super class joe Primate me tho ds #opposable_thumbs
    43. 43. Object joe.opposable_thumbs super class joe Primate me tho ds #opposable_thumbs # => true :( :( :(
    44. 44. Object super Primate super class joe joe'
    45. 45. Object super Primate joe.opposable_thumbs super class joe joe'
    46. 46. Object super Primate joe.opposable_thumbs super class joe joe'
    47. 47. Object super Primate joe.opposable_thumbs super class meth joe joe' ods
    48. 48. Object super Primate joe.opposable_thumbs super class meth joe joe' ods #opposable_thumbs
    49. 49. Object super Primate joe.opposable_thumbs super class meth joe joe' ods #opposable_thumbs # => false :)
    50. 50. Object super meth Primate ods joe.opposable_thumbs super class meth joe joe' ods #opposable_thumbs # => false :)
    51. 51. Object super meth Primate ods #opposable_thumbs joe.opposable_thumbs super class meth joe joe' ods #opposable_thumbs # => false :)
    52. 52. Object super meth Primate ods #opposable_thumbs joe.opposable_thumbs # => true super class meth joe joe' ods #opposable_thumbs # => false :)
    53. 53. Object super class meth bob Primate ods #opposable_thumbs joe.opposable_thumbs # => true super class meth joe joe' ods #opposable_thumbs # => false :)
    54. 54. Object super Primate super Joe’s Singleton Class class joe joe'
    55. 55. Metaclass Singleton Class Eigenclass Virtual Class
    56. 56. Metaclass Singleton Class Eigenclass Virtual Class
    57. 57. bob.class # => Primate
    58. 58. bob.class # => Primate joe.class # => Primate
    59. 59. class Primate class << self def species ["lemur", "monkey", "human"] end end end Primate.species # => ["lemur", ...]
    60. 60. joe = Primate.new def joe.opposable_thumbs false end joe.opposable_thumbs # => false
    61. 61. Primate = Class.new def Primate.species ["lemur", "monkey", "human"] end Primate.species # => ["lemur", "monkey", "human"]
    62. 62. Object super class Primate Class
    63. 63. Object Primate.species super class Primate Class
    64. 64. Object Primate.species super class Primate Class
    65. 65. Object Primate.species super class Primate Class me tho ds
    66. 66. Object Primate.species super class Primate Class me tho ds #species NoMethodError
    67. 67. Object super Class super class Primate Primate'
    68. 68. Object super Class Primate.species super class Primate Primate'
    69. 69. Object super Class Primate.species super class Primate Primate'
    70. 70. Object super Class Primate.species super class meth Primate Primate' ods
    71. 71. Object super Class Primate.species super class meth Primate Primate' ods #species
    72. 72. Object super Class Primate.species super class meth Primate Primate' ods #species # => [“lemur”, ...]
    73. 73. Object super meth ods Class Primate.species super class meth Primate Primate' ods #species # => [“lemur”, ...]
    74. 74. Object super meth ods Class #species Primate.species super class meth Primate Primate' ods #species # => [“lemur”, ...]
    75. 75. Object super Class super Primate’s Singleton Class (Metaclass) class Primate Primate'
    76. 76. a metaclass is a singleton class of a class (to some people, anyways)
    77. 77. super Object Class super super class Primate Primate'
    78. 78. all classes have singletons/metaclasses automatically
    79. 79. super Class super class Object Object' super super class Primate Primate'
    80. 80. super Class super def Object.foo class Object Object' super super class Primate Primate'
    81. 81. super Class super def Object.foo class Object Object' super super Primate.foo class Primate Primate'
    82. 82. super Class super def Object.foo class Object Object' super super Primate.foo meth class ods Primate Primate' #species
    83. 83. super Class super def Object.foo meth ods class Object Object' #foo super super Primate.foo meth class ods Primate Primate' #species
    84. 84. super Module super Class super class Object Object' super class super Primate Primate'
    85. 85. super Module super Class super class Object Object' super super class Primate Primate' super class class joe joe'
    86. 86. super Module super Kernel Class super super class Object Object' super super class Primate Primate' super class class joe joe'
    87. 87. super BasicObject Module 1 .9 super super Kernel Class super super class Object Object' super super class Primate Primate' super class class joe joe'
    88. 88. give me your singleton class!
    89. 89. class Object # The hidden singleton lurks behind everyone def metaclass class << self self end end def meta_eval(&blk) metaclass.instance_eval(&blk) end # Adds methods to a metaclass def meta_def(name, &blk) meta_eval { define_method(name, &blk) } end # Defines an instance method within a class def class_def(name, &blk) class_eval { define_method(name, &blk) } end end http://code.whytheluckystiff.net/metaid/
    90. 90. class Object # The hidden singleton lurks behind everyone def metaclass class << self self end end def meta_eval(&blk) metaclass.instance_eval(&blk) end # Adds methods to a metaclass def meta_def(name, &blk) meta_eval { define_method(name, &blk) } end # Defines an instance method within a class def class_def(name, &blk) class_eval { define_method(name, &blk) } end end http://code.whytheluckystiff.net/metaid/
    91. 91. class Object # The hidden singleton lurks behind everyone def metaclass class << self self end end def meta_eval(&blk) metaclass.instance_eval(&blk) end # Adds methods to a metaclass def meta_def(name, &blk) meta_eval { define_method(name, &blk) } end # Defines an instance method within a class def class_def(name, &blk) class_eval { define_method(name, &blk) } end end http://code.whytheluckystiff.net/metaid/
    92. 92. ruby 1.9 # Object.singleton_class joe.singleton_class # Object#define_singleton_method joe.define_singleton_method(:opposable_thumbs) do false end
    93. 93. def method defines an instance method on Primate class Primate # def defines a method on self def opposable_thumbs true end end
    94. 94. def object.method defines a method on the singleton of Primate class Primate # defines a method on the singleton of self # self is the class Primate def self.species ["Lemur", "Monkey", "Human"] end end
    95. 95. class << o; def defines a method on the singleton of Primate class << Primate # in the singleton class of Primate def species ["Lemur", "Monkey", "Human"] end end
    96. 96. class << class << my_object # in the context of the # singleton class of my_object end
    97. 97. class << class << my_object # in the context of the # singleton class of my_object end class Primate # in the context # of class Primate end
    98. 98. class Foo class << self def bar "hello" end end end Foo.bar # => "hello"
    99. 99. include vs. extend module ThumbsUp def opposable_thumbs; true; end end module Species def species; ["Lemur", "Monkey", "Human"]; end end class Primate # adds methods to the class Primate include ThumbsUp # adds methods to the singleton of Primate extend Species end
    100. 100. instance.extend module ThumbsDown def opposable_thumbs; false; end end joe = Primate.new joe.extend(ThumbsDown)
    101. 101. object.extend(Foo) object.singleton_class.include(Foo)
    102. 102. ?
    103. 103. Brandon Dimcheff http://spkr8.com/t/3756 brandon@dimcheff.com twitter: @bdimcheff http://brandon.dimcheff.com #midwest.rb on irc.freenode.net
    104. 104. Bob - http://en.wikipedia.org/wiki/File:Tamarin_portrait_2_edit3.jpg Joe - http://upload.wikimedia.org/wikipedia/commons/e/ec/Tarsier_Hugs_Mossy_Branch.jpg

    ×