Ruby Quick Reference

Quick reference for efficient coding

View the Project on GitHub YumaYX/RubyQuickReference

read_file.rb(erb)

This Ruby code uses the ERB (Embedded RuBy) library to process an ERB template stored in a file named ‘file.html.erb.’ It creates an ERB object, reads the template file, and sets a variable ‘@val’ to the value ‘val.’ Finally, it evaluates the ERB template with the current binding, and the result is returned.

Execution:

require 'erb'
#=> true

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

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

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

Executed with Ruby 3.3.5