On the _ruby topic of data object in Ruby

Source: Internet
Author: User

Can this code run? What the hell is this data?

require ' Erb '

Data = DATA.Read
Max = 15_000
title = "Hello world!"
content = "Hello world!\ n" * Ten

Max. Times{
 ERB.New(Data). Result(binding)
}

__end__



<body>

<p>
<%= Content%>
</p>
</body>

This magical data is an Io object, read __END__ after the content. One thing to note is that the content will be DATA.read __END__ read out at once, because the IO read feature, when the second time DATA.read the content will be empty, if you need a second read, then the first to execute DATA.rewind .

Sometimes we write a script to do some automated work, need to read the contents of a file in advance, as input or as a template, we can first attach this part of the content __END__ , and then DATA read it, because it is a standard IO object, we can handle the same as normal file processing __END__after the content, as shown in the above code, used to store the ERB content as a template, and then processed, very convenient.

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.