One of the dynamic characteristics of Ruby Way

Source: Internet
Author: User
Tags eval

1 Dynamic Evaluate Code

Global method Eval compiles and executes a string containing a snippet of Ruby code, which is a very strong mechanism because it allows you to build the runtime

Code to execute. Look at the following example:

Ruby Code

Parameters = {}
Argf.each do |line|
name, expr = Line.split (/\s*=\s*/, 2)
parameters[name] = eval EX PR End

Suppose the output is like this:

Reference

A = 1

b = 2 + 3

c = ' Date '

Then your parameters should be like this {"A" =>1, "B" =>5, "C" => "Mon the CDT 21:17:47"}. But in that case, danger

She is very high, so if you pass in a "rm *" then it's depressing.

Ruby has three ways to do it at run time evaluate code, Class_eval, Module_eval, and Instance_eval, the first two are synonymous, and these three

A method actually does the same thing, they are all evaluate a string, or a block, but in doing this, they will

Self changes to their own recipients. If you want to see some examples, it is recommended that you look at Ruby's built-in library delegate.rb.

The Eval method can also invoke evaluate in the creation of local variables outside the surface.

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.