List of Ruby metaprogramming methods?

Just started learning Ruby metaprogramming. Looking at Object.methods, I get:

Object.methods => [ :allocate, :new, :superclass, :freeze, :===, :==, :<=>, :<, :<=, :>, :>=, :to_s, :included_modules, :include?, :name, :ancestors, :instance_methods, :public_instance_methods, :protected_instance_methods, :private_instance_methods, :constants, :const_get, :const_set, :const_defined?, :const_missing, :class_variables, :remove_class_variable, :class_variable_get, :class_variable_set, :class_variable_defined?, :module_exec, :class_exec, :module_eval, :class_eval, :method_defined?, :public_method_defined?, :private_method_defined?, :protected_method_defined?, :public_class_method, :private_class_method, :autoload, :autoload?, :instance_method, :public_instance_method, :nil?, :=~, :!~, :eql?, :hash, :class, :singleton_class, :clone, :dup, :initialize_dup, :initialize_clone, :taint, :tainted?, :untaint, :untrust, :untrusted?, :trust, :frozen?, :inspect, :methods, :singleton_methods, :protected_methods, :private_methods, :public_methods, :instance_variables, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :instance_of?, :kind_of?, :is_a?, :tap, :send, :public_send, :respond_to?, :respond_to_missing?, :extend, :display, :method, :public_method, :define_singleton_method, :__id__, :object_id, :to_enum, :enum_for, :equal?, :!, :!=, :instance_eval, :instance_exec, :__send__] 

Is there a list of methods useful for metaprogramming? Such as instance_eval , initialize and method_missing ?

+6
source share
3 answers

Here is the main answer on this page:

Method hooks

 method_missing method_added singleton_method_added method_removed singleton_method_removed method_undefined singleton_method_undefined 

Class and Module Keys

 inherited append_features included extend_object extended initialize_copy const_missing 

Marshalling Hooks

 marshal_dump marshal_load 

Coercion hooks

 coerce induced_from to_xxx 

Also check this blog for explanations and sample code for many of these methods.

+8
source

Object is a class, so most of the methods you specify are actually Class instance methods. Look also at Object.private_methods - you will find define_method there, which is absolutely necessary. But binding also very powerful ... when you learn about Ruby metaprogramming, you need to look at the documents for the binding class. The send , __send__ and public_send also essential.

By looking at the list above, you can identify the β€œreflection” methods that can be used to query and manage constants, methods, instance variables, etc. programmatically. Then there are eval and exec methods. method_missing is one of the first ones you are learning, but pay attention also to const_missing . Be sure to check set_trace_func and trace_var . And where would we be without alias and alias_method ?

Then there is a hooks interpreter such as method_added , method_removed , method_undefined , inherited , extended , included , singleton_method_added , singleton_method_removed , singleton_method_undefined , instance_method_added , instance_method_removed and instance_method_undefined .

Object#method is essential for getting Method objects. Take a look at all Method methods, including things like owner . Kernel#caller can be useful sometimes. Then also find the ObjectSpace class.

Understand that although they have some special behavior, classes and modules are just objects, and you can create them dynamically, store them in data structures, etc. They don’t even need to have names. You can simply call Class.new to create a new one, and use class_eval , define_method , etc., if necessary, to add methods to it.

__LINE__ and __FILE__ can be interesting, especially File.read(__FILE__) .

Understanding blocks and lambdas is important both for general Ruby programming and for metaprogramming in particular.

+5
source

In the book Rubin Metaprogramming, the author claims that there is no clear line between metaprogramming and programming. All this is just programming.

Some examples I can think of are some intermediate programming and metaprogramming: class and attr_reader .

+2
source

Source: https://habr.com/ru/post/909588/


All Articles