Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Meta programming

  • Login to see the comments

Meta programming

  1. 1. by @antho1404 Meta programming - Reflexive programming
  2. 2. "Capacité d'un programme à examiner et  modifier ses objets lors de son exécution"
  3. 3. What's an object ? - attributes - methods - parent object - private - protected - public
  4. 4. Introspection:  "L’introspection est la capacité pour un objet de s’auto-examiner"
  5. 5. Some usefull methods: :methods                         # list of methods defined :public_methods             # list of public methods :private_methods            # list of private methods :protected_methods         # list of protected methods :instance_variables         # list of instance variables :instance_variable_get    # get value of instance variable     
  6. 6. Exemple: class A     @loutre     def initialize         @loutre = 23     end     def update_loutre          @loutre += 1      end end a = A . new  # create a new class # get the value of the attribute a.instance_variable_get "@loutre" # => 23 # call the method update_loutre a.method( "update_loutre" ).call # => 24 # get tue value of the attribute a.instance_variable_get "@loutre" # => 24
  7. 7. Intercession:  "L'intercession est la capacité pour un objet de s’auto-modifier"
  8. 8. :define_method                     # define a new method :remove_method                   # remove a method :instance_variable_set          # change the value of an variable :remove_instance_variable   # remove an instance variable Some usefull methods:
  9. 9. class A     # exemple of method which create methods to select an attribute on a class      def select ( table, attribute )                   # define the method for this class using the attribute and table parameters          self.class.send   :define_method , "select_#{attribute}_on_#{table}"   do                           # use the table and transform it into an object              model = table.classify.constantize                           # select all the value of the attributes given in the function name              model.find :all , :select => attribute         end      end end a = A . new                                  # create the class a.select "user", "name"             # call the method which define a new method a.select_name_on_player         # call the new created method Exemple:
  10. 10. Meta programming in other languages Python Java Objective-C PHP4 et + C# Ruby
  11. 11. http://www.infoq.com/presentations/metaprogramming-ruby http://ola-bini.blogspot.com/2006/09/ruby-metaprogramming-techniques.html http://www.devsource.com/c/a/Languages/An-Exercise-in-Metaprogramming-with-Ruby http://buhrmi.tumblr.com/post/8035284845/embracing-meta-programming-an-rpg-character-stat http://weare.buildingsky.net/2009/08/25/rubys-metaprogramming-toolbox http://www.vitarara.org/cms/ruby_metaprogamming_declaratively_adding_methods_to_a_class http://ruby-metaprogramming.rubylearning.com/ Some links to continue
  12. 12. @antho1404 antho1404 www.quidizz.com @quidizz Quidizz

×