Ruby meta-programming summary, ruby Summary

Source: Internet
Author: User

Ruby meta-programming summary, ruby Summary

When I was asked such questions today, I used to think that this is a broad concept. Even if I have used these features, but I still don't know this is called "metaprogramming" until today when I was asked, I just had an epiphany, and then I made a small summary on the Internet with some of my actual metaprogramming.

The so-called meta-programming in Ruby is a technology that can dynamically operate the language structure (such as classes, modules, instance variables, etc.) at runtime. You can even directly type a new Ruby code at runtime without restarting it and execute it.

Ruby's metaprogramming also plays the role of "using code to write code. For example, the common methods such as attr_accessor are the same.

First of all, I can think of the following:

1, respond_to?
2, define_method
3, instance_variable_get, instance_variable_set
4. the eval method can be avoided as much as possible, and the execution efficiency is very low !!

The specific meaning must be that you can see the logic to be implemented by the method name.
For details, see the http://api.rubyonrails.org/specific analysis on the source code, very simple.

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

1, introspection, reflection

In Ruby, you have the ability to view information about classes or objects at runtime. We can use class, instance_methods, intance_variables, and other methods to achieve our goal. We will talk about this technology as Introspection or Reflection ).

The language used to compile the metacharacter program is called the metacharacter language. The language of the manipulated program is called the target language. A programming language is also called reflection or self-inversion. -- Taken from Wikipedia meta-programming entries.

The Code is 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? The method is another useful method in the reflection mechanism. Use respond_to? Method, you can know in advance whether the object can process the information you want to submit and execute with it. All objects have this method. Use 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, which can be called by an instance. It provides a way to operate on instance variables of objects. You can use a string to pass parameters or a code block to this method.
The Code is as follows:
ClassRubyist
Definitialize
@ Geek = "Matz"
End
End
Obj = Rubyist. new

# Instance_eval can manipulate the private method of obj and instance variables

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


The code block passed through instance_eval allows you to operate inside the object. You can manipulate the object without any data being private! Instance_eval can also be used to add class methods.

The Code is 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 operate constants. Const_get returns the value of the specified constant:
The Code is as follows:
Puts Float. const_get (: MIN) # => 2.225074255072e-308

Const_set sets the specified value for the specified constant and returns this object. If the constant does not exist, the constant will be created as shown in the following example:
The Code is as follows:
ClassRubyist
End
Puts Rubyist. const_set ("PI", 22.0/7.0) # => 3.14285714285714

Because const_get returns the constant value, you can use this method to obtain the name of a class and add a new method for instantiating an object for this class. This allows us to create classes at runtime and instantiate their instances.
The Code is as follows:
# Let us call our new class 'rubyist'
# (We cocould have prompted the user for a class name)
Class_name = "rubyist". capitalize
Object. const_set (class_name, Class. new)
# Let us create a method 'who'
# (We cocould 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.