Analysis of DATA Objects in Ruby and RubyDATA objects
Can this code be run? What is this DATA?
Require 'erb' data = DATA. readmax = 15_000title = "hello world! "Content =" hello world! \ N "* 10max. times {ERB. new (data ). result (binding )} __end __< html>
This magic DATA is an IO object, read_ END __
Content. Note thatDATA. read
Will_ END __
The subsequent content is read at one time. Due to the IO reading feature, whenDATA. read
The content will be empty. If you need to read the content for the second time, you must first executeDATA. rewind
.
Sometimes we write a script to do some automation work. We need to read the content of a file in advance. As an input or as a template, We can first attach this part of content_ END __
And then useDATA
Because it is a standard IO object, we can process it like a normal file._ END __
Content, as shown in the code above, used to storeERB
It is very convenient to use the content as a template and then process it.