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
(Embedded Ruby) template engine. It reads an ERB file (file.html.erb
), defines a Ruby variable @val
, and then renders the ERB template using the current object’s binding, effectively substituting the variable into the template and producing a string output. The require 'erb'
line loads the ERB library, and erb.result(binding)
executes the ERB template with access to the local variables.
Ruby code snippet
require 'erb'
#=> true
erb = ERB.new(File.read('input/erb/file.html.erb'))
#=>
#<ERB:0x00007fc40da48c00
@val = 'val'
#=> "val"
erb.result(binding)
#=> "<h1>header</h1>\n\n<p>val</p>\n"
Executed with Ruby 3.4.5
.