Heads up: This description was created by AI and might not be 100% accurate.

read_file.rb

This Ruby code snippet demonstrates the use of the ERB module to parse an HTML template file and generate a new HTML file with the dynamic data injected.

The first line of code, require 'erb', loads the ERB module from the standard library.

The second line of code, erb = ERB.new(File.read('input/erb/file.html.erb')), creates a new instance of the ERB class and reads the contents of the file 'input/erb/file.html.erb' into memory. The ERB class is used to parse HTML templates with embedded Ruby code, allowing you to inject dynamic data into the template.

The third line of code, @val = 'val', sets a variable named @val to the string 'val'. This variable will be used in the template to demonstrate how to insert dynamic data into the HTML output.

The fourth line of code, erb.result(binding), uses the result method of the ERB instance to generate an HTML file from the template. The binding object is passed as an argument to the result method, which allows the template to access variables defined in the surrounding context (in this case, the @val variable).

The generated HTML file will contain the dynamic data from the @val variable inserted into the template. For example, if the value of @val is 'hello', the resulting HTML file would contain the text ‘hello’ somewhere within it.

Ruby code snippet

require 'erb'
#=> true

erb = ERB.new(File.read('input/erb/file.html.erb'))
#=> 
#<ERB:0x00007fcd53830518

@val = 'val'
#=> "val"

erb.result(binding)
#=> "<h1>header</h1>\n\n<p>val</p>\n"

Executed with Ruby 3.4.4.