SlideShare a Scribd company logo
Goal: to grok this code
class Post < ActiveRecord::Base <= class extension mixin
 belongs_to :user               <= macro
end

my_post = Post.new
my_post.name = 'Chile L‘          <= employs #method_missing
my_post.subject = 'traveling in thailand'
my_post.content = 'sometimes you can almost not tell the
difference'
my_post.save!
Coming from a compiled
language, runtime execution is
  foreign, all code is executed
               code

    Post.respond_to? Name


      Ruby is a dynamic language
Classes are also like namespaces
               too.
class MyClass

 puts ‘Hello World’

end

# => Hello World
Eigenclasses
Singleton method variations on
     the MyClass instance
  class MyClass
   def MyClass.my_method; end
   def self.my_method; end

   class << self
    def my_method; end
   end
  end
class << Object
 puts self
 def my_method                     Different
  puts ‘hi’
 end
end

class Object           class << Object is adding methods to the eigenclass of Object.
                       This method is now available to all objects as if it were a class
 def self.meth2
  puts self            method.
  puts ‘there’
 end
                       However, opening the Object class and adding methods to self
 class << self         shows the current object is the instance Foo.
  def meth3
    puts self
    puts ‘hahaha’
  end
 end
end
Foo = Class.new
Foo.my_method
Foo.meth2
Foo.meth3
# => #<Class:Object>
# => hi
# => Foo
# => there
# => Foo
# => hahaha
Including the module in the Eigenclass of
     class C to create class methods
   module M
    def my_method
     puts ‘hi’
    end
   end

   class C
    class << self
     include M
    end
   end

   C.my_method      # => hi
Using extend to add class methods to C
      module M
       def my_method
         puts ‘hi’
       end
      end
      class C
       extend M
      end

      C.my_method      # => hi
Class methods are singletons of
          the class object, that means they
                 are in the eigenclass
class C             Self, therefore the current object, is
 puts self          changed by opening up the
 class << self      eigenclass
   puts self
   def my_method
    puts ‘hi’
   end
 end
end
C.my_method # => C
               # => #<Class:C>
               # => hi
Examining self…

class Foo           Self, the current object, is changed
 puts self          by opening up the eigenclass and
 class << self      we are now adding my_method to
   puts self        the Foo instance, but this is
   def my_method    equivalent to Foo.my_method
    puts ‘hi’
   end
 end
end
Foo.my_method # => Foo
                 # => #<Class:Foo>
                 # => hi
Hook methods or
   callbacks
Hooking

Module#included exists solely
as a callback and the default
implementation is empty.
Hook methods - Module#included
  module Mod1
   class << self
    def included(othermod)
      puts "Mod1 was mixed into #{othermod}"
    end
   end
  end

  class MyClass
   include Mod1
  end
  # => Mod1 was mixed into MyClass
Hook methods - Module#include
(must call super if you override, to actually include
                     the module)
    module Mod1; end
    module Mod2; end

    class MyClass
     def self.include(*modules)
      puts “#{modules} included”
      super
     end
     include Mod1
     include Mod2
    end
    # => [Mod1] included
    # => [Mod2] included
Putting stuff together…
module Associations                     This is obviously very dumbed down, but
                                         now we can start to see some things in
  def self.included(base)                action.
   base.extend(ClassMethods)
  end                                    We have defined the module
                                         Associations which includes a submodule
  module ClassMethods                    ClassMethods which will hold class
    def belongs_to                       methods.
     “I am in the ClassMethods module”
    end                                Overriding included now acts on the
  end                                  including class which is base in this case.
 end                                   This class is sometimes called the
                                       inclusor. The extend method adds the
 class Base                            ClassMethods to the inclusor’s
  include Associations                 eigenclass.
 end
                                       Now the Base class can include the
class Post < Base                      Associations module and our model can
 belongs_to                            inherit from Base
end
                                       One more step…
module ActiveRecord
 module Associations

  def self.included(base)
   base.extend(ClassMethods)
  end

  module ClassMethods
   def belongs_to
    “I am in the ClassMethods module”
   end
  end
 end

 class Base
  include Associations
 end
end

class Post < ActiveRecord::Base
 belongs_to
end

Add an ActiveRecord Namespace and we have something
resembling the original
Ghost Methods
class Post                                  #method_missing
 def initialize                             catches the call to
  @attributes = {}
                                            name=() or returns
 end
 def method_missing(name, *args)            the value when it
  attribute = name.to_s                     gets a method
  if attribute =~ /=$/                      without an ‘=‘
    @attributes[attribute.chop] = args[0]
  else                                      It chops off the
    @attributes[attribute]
                                            equals sign to get
  end
 end                                        the attribute name
end                                         and then sets the
my_post = Post.new                          hash value
my_post.name = ‘Chile L’

More Related Content

PPTX
Only oop
PDF
The Ruby Object Model by Rafael Magana
PPT
Php object orientation and classes
PPTX
Inheritance Mixins & Traits
PPTX
Advance OOP concepts in Python
PDF
Ruby object model at the Ruby drink-up of Sophia, January 2013
PPT
Introduction to OOP with PHP
Only oop
The Ruby Object Model by Rafael Magana
Php object orientation and classes
Inheritance Mixins & Traits
Advance OOP concepts in Python
Ruby object model at the Ruby drink-up of Sophia, January 2013
Introduction to OOP with PHP

What's hot (20)

PPTX
Object oriented programming in php
PPTX
Object oreinted php | OOPs
PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PPTX
Java Inheritance
PPTX
Variables in python
PPT
Oops concepts in php
PPTX
Ppt on this and super keyword
PDF
Ruby — An introduction
PPTX
Inheritance in java
PPTX
Php oop presentation
PPTX
Python advance
PPTX
Inheritance in java
PPTX
Classes, objects in JAVA
PPT
Inheritance in java
PPTX
Object oriented programming in php 5
PPTX
Inheritance in java
PDF
Python programming : Inheritance and polymorphism
PDF
Demystifying oop
PPTX
Python: Basic Inheritance
Object oriented programming in php
Object oreinted php | OOPs
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
Java Inheritance
Variables in python
Oops concepts in php
Ppt on this and super keyword
Ruby — An introduction
Inheritance in java
Php oop presentation
Python advance
Inheritance in java
Classes, objects in JAVA
Inheritance in java
Object oriented programming in php 5
Inheritance in java
Python programming : Inheritance and polymorphism
Demystifying oop
Python: Basic Inheritance
Ad

Viewers also liked (7)

PDF
02 elements of vectors
DOCX
Danzastipicas (1)
PPTX
Blogujeme vo wordpresse
PDF
Infografia danzastipicas
PPTX
Zmeňte svet slovom
PDF
Infografia danzastipicas
PPTX
General Psych, Ch. 7
02 elements of vectors
Danzastipicas (1)
Blogujeme vo wordpresse
Infografia danzastipicas
Zmeňte svet slovom
Infografia danzastipicas
General Psych, Ch. 7
Ad

Similar to Lightning talk (20)

PPTX
Ruby object model
PDF
Metaprogramming 101
PDF
Object_oriented_programming_OOP_with_PHP.pdf
PPT
Ruby Metaprogramming
PDF
Metaprogramming in Ruby
KEY
Metaprogramming
PDF
The Dark Art of Rails Plugins (2008)
PPT
Introduction to Python - Part Three
PDF
Ruby Metaprogramming
PDF
Classboxes, nested methods, and real private methods
PPTX
Ruby object model - Understanding of object play role for ruby
KEY
Reusable Ruby • Rt 9 Ruby Group • Jun 2012
KEY
Module Magic
PDF
13 ruby modules
PPTX
Introduction to Ruby’s Reflection API
PDF
Extending Rails with Plugins (2007)
PPTX
Application package
PDF
Metaprogramming Rails
PPTX
Python – Object Oriented Programming
PPTX
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx
Ruby object model
Metaprogramming 101
Object_oriented_programming_OOP_with_PHP.pdf
Ruby Metaprogramming
Metaprogramming in Ruby
Metaprogramming
The Dark Art of Rails Plugins (2008)
Introduction to Python - Part Three
Ruby Metaprogramming
Classboxes, nested methods, and real private methods
Ruby object model - Understanding of object play role for ruby
Reusable Ruby • Rt 9 Ruby Group • Jun 2012
Module Magic
13 ruby modules
Introduction to Ruby’s Reflection API
Extending Rails with Plugins (2007)
Application package
Metaprogramming Rails
Python – Object Oriented Programming
c91632a4-2e92-4edf-b750-358da15ed1b1.pptx

Recently uploaded (20)

PPTX
Ascension Descend, Chakra, Kundalini, Light, Twin Flames all connected.pptx
PPTX
cristianity quiz.pptx introduction to world religion
PDF
Life of Saint John Gabriel Perboyre, C.M.
PDF
Printable Thai Gospel Tract - Be Sure of Heaven.pdf
PPTX
The Neuroscience of Manifestation: How Top Leaders Use The Law of Attraction ...
PDF
He Bore the Sin of Many - part 1
PPTX
Art of smart work Bhagavat Gita knowledge
PPTX
The Biography of Walter Rea walter .pptx
PPTX
What is Christianity and the whole history
PDF
Printable Sinhala Gospel Tract - Be Sure of Heaven.pdf
PPTX
Sabbath School Lesson 7, 3rd Quarter 2025.pptx
PPTX
God, His Creation, His Game and Service to Him.pptx
PPTX
Biography of frederick wheeler and John Andrews.pptx
PDF
Krishna’s 8 Symbols and What They Represent
PPTX
Part 1A Time - Not Linear Its Cyclic Spiral.pptx
PPTX
391 Do good to your servant according to your word LORD 392 Full Redemption
PDF
Printable Upper Sorbian Gospel Tract - Be Sure of Heaven.pdf
PPTX
Article--Non-Narrated--Davidson_The_Biblical_Account_Of_Origins_Long.pptx
PPTX
Organizational Psychology Advance Notes.pptx
PPTX
Why God? a Beginning Course on Apologetics - Part 1
Ascension Descend, Chakra, Kundalini, Light, Twin Flames all connected.pptx
cristianity quiz.pptx introduction to world religion
Life of Saint John Gabriel Perboyre, C.M.
Printable Thai Gospel Tract - Be Sure of Heaven.pdf
The Neuroscience of Manifestation: How Top Leaders Use The Law of Attraction ...
He Bore the Sin of Many - part 1
Art of smart work Bhagavat Gita knowledge
The Biography of Walter Rea walter .pptx
What is Christianity and the whole history
Printable Sinhala Gospel Tract - Be Sure of Heaven.pdf
Sabbath School Lesson 7, 3rd Quarter 2025.pptx
God, His Creation, His Game and Service to Him.pptx
Biography of frederick wheeler and John Andrews.pptx
Krishna’s 8 Symbols and What They Represent
Part 1A Time - Not Linear Its Cyclic Spiral.pptx
391 Do good to your servant according to your word LORD 392 Full Redemption
Printable Upper Sorbian Gospel Tract - Be Sure of Heaven.pdf
Article--Non-Narrated--Davidson_The_Biblical_Account_Of_Origins_Long.pptx
Organizational Psychology Advance Notes.pptx
Why God? a Beginning Course on Apologetics - Part 1

Lightning talk

  • 1. Goal: to grok this code class Post < ActiveRecord::Base <= class extension mixin belongs_to :user <= macro end my_post = Post.new my_post.name = 'Chile L‘ <= employs #method_missing my_post.subject = 'traveling in thailand' my_post.content = 'sometimes you can almost not tell the difference' my_post.save!
  • 2. Coming from a compiled language, runtime execution is foreign, all code is executed code Post.respond_to? Name Ruby is a dynamic language
  • 3. Classes are also like namespaces too. class MyClass puts ‘Hello World’ end # => Hello World
  • 5. Singleton method variations on the MyClass instance class MyClass def MyClass.my_method; end def self.my_method; end class << self def my_method; end end end
  • 6. class << Object puts self def my_method Different puts ‘hi’ end end class Object class << Object is adding methods to the eigenclass of Object. This method is now available to all objects as if it were a class def self.meth2 puts self method. puts ‘there’ end However, opening the Object class and adding methods to self class << self shows the current object is the instance Foo. def meth3 puts self puts ‘hahaha’ end end end Foo = Class.new Foo.my_method Foo.meth2 Foo.meth3 # => #<Class:Object> # => hi # => Foo # => there # => Foo # => hahaha
  • 7. Including the module in the Eigenclass of class C to create class methods module M def my_method puts ‘hi’ end end class C class << self include M end end C.my_method # => hi
  • 8. Using extend to add class methods to C module M def my_method puts ‘hi’ end end class C extend M end C.my_method # => hi
  • 9. Class methods are singletons of the class object, that means they are in the eigenclass class C Self, therefore the current object, is puts self changed by opening up the class << self eigenclass puts self def my_method puts ‘hi’ end end end C.my_method # => C # => #<Class:C> # => hi
  • 10. Examining self… class Foo Self, the current object, is changed puts self by opening up the eigenclass and class << self we are now adding my_method to puts self the Foo instance, but this is def my_method equivalent to Foo.my_method puts ‘hi’ end end end Foo.my_method # => Foo # => #<Class:Foo> # => hi
  • 11. Hook methods or callbacks
  • 12. Hooking Module#included exists solely as a callback and the default implementation is empty.
  • 13. Hook methods - Module#included module Mod1 class << self def included(othermod) puts "Mod1 was mixed into #{othermod}" end end end class MyClass include Mod1 end # => Mod1 was mixed into MyClass
  • 14. Hook methods - Module#include (must call super if you override, to actually include the module) module Mod1; end module Mod2; end class MyClass def self.include(*modules) puts “#{modules} included” super end include Mod1 include Mod2 end # => [Mod1] included # => [Mod2] included
  • 16. module Associations This is obviously very dumbed down, but now we can start to see some things in def self.included(base) action. base.extend(ClassMethods) end We have defined the module Associations which includes a submodule module ClassMethods ClassMethods which will hold class def belongs_to methods. “I am in the ClassMethods module” end Overriding included now acts on the end including class which is base in this case. end This class is sometimes called the inclusor. The extend method adds the class Base ClassMethods to the inclusor’s include Associations eigenclass. end Now the Base class can include the class Post < Base Associations module and our model can belongs_to inherit from Base end One more step…
  • 17. module ActiveRecord module Associations def self.included(base) base.extend(ClassMethods) end module ClassMethods def belongs_to “I am in the ClassMethods module” end end end class Base include Associations end end class Post < ActiveRecord::Base belongs_to end Add an ActiveRecord Namespace and we have something resembling the original
  • 18. Ghost Methods class Post #method_missing def initialize catches the call to @attributes = {} name=() or returns end def method_missing(name, *args) the value when it attribute = name.to_s gets a method if attribute =~ /=$/ without an ‘=‘ @attributes[attribute.chop] = args[0] else It chops off the @attributes[attribute] equals sign to get end end the attribute name end and then sets the my_post = Post.new hash value my_post.name = ‘Chile L’