Read_file.rb

This content was produced by an LLM and could include errors.

このスクリプトはERBテンプレート処理の標準的な流れです。ERB.newfile.html.erb をコンパイルし、インスタンス変数 @val = 'val' を設定したうえで result(binding) を呼ぶと、テンプレート中の <%= @val %> が評価され <p>val</p> を含むHTML文字列が生成されます。テンプレートへ値を渡すには binding の受け渡しが鍵になります。

require 'erb'
#=> true

erb = ERB.new(File.read('input/erb/file.html.erb'))
#=> #<ERB:0x00007f6cec2f9710
#...
@val = 'val'
#=> "val"

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

Ruby 4.0.6