Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

erb

Package Overview
Dependencies
Maintainers
2
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

erb

bundlerRubyGems.org
Version
6.0.1
Version published
Maintainers
2
Created
Source

ERB (Embedded Ruby)

ERB is an easy-to-use, but also very powerful, template processor.

ERB is commonly used to produce:

  • Customized or personalized email messages.
  • Customized or personalized web pages.
  • Software code (in code-generating applications).

Like method sprintf, ERB can format run-time data into a string. ERB, however, is much more powerful

How ERB Works

Using ERB, you can create a template: a plain-text string that has specially-formatted tags, then store it into an ERB object; when ERB produces result string, it:

  • Inserts run-time-evaluated expressions into the result.
  • Executes snippets of Ruby code.
  • Omits comments from the results.

In the result:

  • All non-tag text is passed through, unchanged.
  • Each tag is either replaced (expression tag), or omitted entirely (execution tag or comment tag).

There are three types of tags:

TagFormActionText in Result
Expression tag'<%= ruby_expression %>'Evaluates ruby_expression.Value of expression.
Execution tag'<% ruby_code %>'Execute ruby_code.None.
Comment tag'<%# comment_text %>'None.None.

These examples use erb, the ERB command-line interface; each "echoes" a string template and pipes it to erb as input:

  • Expression tag:

      $ echo "<%= $VERBOSE %>" | erb
      "false"
      $ echo "<%= 2 + 2 %>" | erb
      "4"
    
  • Execution tag:

      echo "<% if $VERBOSE %> Long message. <% else %> Short message. <% end %>" | erb
      " Short message. "
    
  • Comment tag:

      echo "<%# TODO: Fix this nonsense. %> Nonsense." | erb
      " Nonsense."
    

How to Use ERB

You can use ERB either:

  • In a program: see class ERB.
  • From the command line: see ERB Executable.

Installation

ERB is installed with Ruby, and so there's no further installation needed.

Other Template Engines

There are a variety of template engines available in various Ruby projects. For example, RDoc, distributed with Ruby, uses its own template engine, which can be reused elsewhere.

Other popular template engines may be found in the Ruby Toolbox.

Code

The ERB source code is in GitHub project ruby/erb.

Bugs

Bugs may be reported at ERB Issues.

License

This software is available as open source under the terms of the 2-Clause BSD License.

FAQs

Package last updated on 15 Dec 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts