Ruby Meta-programming-code for writing code

Source: Internet
Author: User

Kernel#eval () method, you can directly execute Ruby code string

(1) The Kernel#binding () method is used to capture the current scope to return a binding object that represents a complete scope

(2) eval () can accept two parameters, the first is a code string, the second is a binding object, and the code executes in the scope of the pass-through (the *eval () family can)

(3) Ruby also provides a predefined constant named Toplevel_binding, which represents the binding object for the top-level scope. This top-level scope can be accessed from anywhere in the program

(4) The Eval () method will have two optional parameters, file and line, the passed-in file path, and the current execution code row number, for printing out stack information in the case of a code exception

(5) because the code string and fast very similar, in many cases, you can choose to use any one, but can use blocks as much as possible.

(6) Eval () method has the risk of code injection attack, use caution

(7) Pollution object and safety level:

The pollution object includes the string that the program reads from the Web form, the File box command line, even the system variables, each time the new string from the pollution string operation, is also contaminated, can call tainted? () method to determine if it is contaminated

Ruby security level, can be changed by modifying $safe global scalar, divided into 0-4, 5 levels

0--is completely unconstrained, greater than 0,ruby will refuse to execute a contaminated string, greater than 2, will prohibit a large portion of file-related work

Contamination can be removed by Object#untaint () method


Ruby Meta-programming-code for writing code

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.