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. 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
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. 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. Meta programming in other languages Python Java Objective-C PHP4 et + C# Ruby