Ruby Meta Programming summary _ruby Special topics

Source: Internet
Author: User
Tags constant reflection


When asked this question today, it was always considered a broader concept, even though I have used these characteristics, but have never known this is called "meta programming" until today when people ask, just epiphany some, and then on the Internet and their own flat practical some metaprogramming make a small sum.



The so-called metaprogramming in Ruby is the technique of manipulating language structures (such as classes, modules, instance variables, etc.) dynamically at runtime. You can even type a new Ruby code at run time without restarting, and execute it.



Ruby's metaprogramming also has the function of "coding with code". This is true, for example, with common Attr_accessor and other methods.

The first thing I can think of is that I've used a few of them:



1,respond_to?
2,define_method
3,instance_variable_get, Instance_variable_set
4,eval but this method can avoid as far as possible, the execution efficiency is extremely low!!



The exact meaning must be seen in the method name to know the logic of these methods to achieve.
Specific details can be detailed in http://api.rubyonrails.org/analysis of the source code, very simple.

Here are my search from other places to help improve my programming skills in future work;

1, introspection, reflection



In Ruby, you are fully capable of viewing information about a class or object at run time. We can use class, Instance_methods, Intance_variables and other methods to achieve the goal. We say this technique becomes introspective (introspection) or reflex (Reflection).



The language that writes the META program is called the Meta language. The language of the manipulated program is called the target language. A programming language is also the ability of its own meta language to be called reflection or reflexive. --excerpt from the Wikipedia Meta programming entry.





Copy Code code as follows:

Classrubyist
Defwhat_does_he_do
@person = ' A rubyist '
' Ruby programming '
End
End

An_object = Rubyist.new
Puts an_object.class# => rubyist
Puts An_object.class.instance_methods (false) # => WHAT_DOES_HE_DO
An_object.what_does_he_do
Puts an_object.instance_variables# => @person


Respond_to? method is another useful method in the reflection mechanism. Using the Respond_to method, you can know in advance whether an object can handle the information you want to perform with him. All objects have this method, using the Respond_to method, you can determine whether the object can use the specified method.

2,instance_eval
The object class provides a public method named Instance_eval that can be invoked by one instance. He provides a way to manipulate an instance variable of an object. You can use a string to pass arguments to this method or pass a code block.
 code as follows:

Classrubyist
Definitialize
@geek = "Matz"
End
End
obj = rubyist.new

# instance_eval can manipulate obj's private methods and instance variables

Obj.instance_evaldo
putsself# => #puts @geek # => Matz
End


A block of code passed through the Instance_eval allows you to manipulate within the object. You can manipulate within the object, no longer have any data is private! Instance_eval can also be used to add class methods.







code as follows:

Classrubyist
End

Rubyist.instance_evaldo
Defwho
"Geek"
End
End

Puts rubyist.who# => Geek


Const_get, Const_set

Similarly, const_get and const_set are used to manipulate constants. Const_get returns the value of the specified constant:
 code as follows:

Puts Float.const_get (: MIN) # => 2.2250738585072e-308

Const_set sets the specified value for the specified constant, and returns the object. If the constant does not exist, then he creates the constant, which is the following example:
 code as follows:

Classrubyist
End
Puts Rubyist.const_set ("PI", 22.0/7.0) # => 3.14285714285714

Because Const_get returns the value of a constant, you can use this method to get the name of a class and add a new instantiation object to the class. This gives us the ability to create a class and instantiate its instance at run time.
 code as follows:

# Let us call our new class ' Rubyist '
# (We could have prompted the user for a class name)
class_name = "Rubyist". Capitalize
Object.const_set (class_name,class.new)
# Let us create a "who"
# (We could have prompted the user for a method name)
Class_name =object.const_get (class_name)
Puts class_name# => rubyist
Class_name.class_evaldo
Define_method:whodo|my_arg|
My_arg
End
End
obj = class_name.new
Puts obj.who (' Matz ') # => Matz




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.