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.