class ERB
Class ERB (the name stands for Embedded Ruby) is an easy-to-use, but also very powerful, template processor.
Like method sprintf, ERB can format run-time data into a string. ERB, however,s is much more powerful.
ERB is commonly used to produce:
-
Customized or personalized email messages.
-
Customized or personalized web pages.
-
Software code (in code-generating applications).
Usage¶ ↑
Before you can use ERB, you must first require it (examples on this page assume that this has been done):
require 'erb'
In Brief¶ ↑
Here’s how ERB works:
-
You can create an ERB object (a template) to store text that includes specially formatted tags.
-
You can call instance method
ERB#result
to get the result.
ERB supports tags of three kinds:
-
Expression tags: each begins with
'<%'
, ends with'%>'
; contains a Ruby expression; in the result, the value of the expression replaces the entire tag:magic_word = 'xyzzy' template.result(binding) # => "The magic word is xyzzy." ERB.new('Today is <%= Date::DAYNAMES[Date.today.wday] %>.').result # => "Today is Monday."
The first call to
result
passes argumentbinding
, which contains the binding of variablemagic_word
to its string value'xyzzy'
.The second call need not pass a binding, because its expression
Date::DAYNAMES
is globally defined. -
Execution tags: each begins with
'<%='
, ends with'%>'
; contains Ruby code to be executed:s = '<% File.write("t.txt", "Some stuff.") %>' ERB.new(s).result File.read('t.txt') # => "Some stuff."
-
Comment tags: each begins with
'<%#'
, ends with'%>'
; contains comment text; in the result, the entire tag is omitted.s = 'Some stuff;<%# Note to self: figure out what the stuff is. %> more stuff.' ERB.new(s).result # => "Some stuff; more stuff."
Some Simple Examples¶ ↑
Here’s a simple example of ERB in action:
s = 'The time is <%= Time.now %>.' template = ERB.new(s) template.result # => "The time is 2025-09-09 10:49:26 -0500."
Details:
-
A plain-text string is assigned to variable
s
. Its embedded expression tag'<%= Time.now %>'
includes a Ruby expression,Time.now
. -
The string is put into a new ERB object, and stored in variable
template
. -
Method call
template.result
generates a string that contains the run-time value ofTime.now
, as computed at the time of the call.
The template may be re-used:
template.result # => "The time is 2025-09-09 10:49:33 -0500."
Another example:
s = 'The magic word is <%= magic_word %>.' template = ERB.new(s) magic_word = 'abracadabra' # => "abracadabra" template.result(binding) # => "The magic word is abracadabra."
Details:
-
As before, a plain-text string is assigned to variable
s
. Its embedded expression tag'<%= magic_word %>'
has a variable name,magic_word
. -
The string is put into a new ERB object, and stored in variable
template
; note thatmagic_word
need not be defined before the ERB object is created. -
magic_word = 'abracadabra'
assigns a value to variablemagic_word
. -
Method call
template.result(binding)
generates a string that contains the value ofmagic_word
.
As before, the template may be re-used:
magic_word = 'xyzzy' template.result(binding) # => "The magic word is xyzzy."
Bindings¶ ↑
A call to method result
, which produces the formatted result string, requires a Binding object as its argument.
The binding object provides the bindings for expressions in expression tags.
There are three ways to provide the required binding:
Default Binding
¶ ↑
When you pass no binding
argument to method result
, the method uses its default binding: the one returned by method new_toplevel
. This binding has the bindings defined by Ruby itself, which are those for Ruby’s constants and variables.
That binding is sufficient for an expression tag that refers only to Ruby’s constants and variables; these expression tags refer only to Ruby’s global constant RUBY_COPYRIGHT
and global variable $0
:
s = <<EOT The Ruby copyright is <%= RUBY_COPYRIGHT.inspect %>. The current process is <%= $0 %>. EOT puts ERB.new(s).result The Ruby copyright is "ruby - Copyright (C) 1993-2025 Yukihiro Matsumoto". The current process is irb.
(The current process is irb
because that’s where we’re doing these examples!)
Local Binding
¶ ↑
The default binding is not sufficient for an expression that refers to a a constant or variable that is not defined there:
Foo = 1 # Defines local constant Foo. foo = 2 # Defines local variable foo. s = <<EOT The current value of constant Foo is <%= Foo %>. The current value of variable foo is <%= foo %>. The Ruby copyright is <%= RUBY_COPYRIGHT.inspect %>. The current process is <%= $0 %>. EOT
This call raises NameError
because although Foo
and foo
are defined locally, they are not defined in the default binding:
ERB.new(s).result # Raises NameError.
To make the locally-defined constants and variables available, you can call result
with the local binding:
puts ERB.new(s).result(binding) The current value of constant Foo is 1. The current value of variable foo is 2. The Ruby copyright is "ruby - Copyright (C) 1993-2025 Yukihiro Matsumoto". The current process is irb.
Augmented Binding
¶ ↑
Another way to make variable bindings (but not constant bindings) available is to use method result_with_hash(hash)
; the passed hash has name/value pairs that are to be used to define and assign variables in a copy of the default binding:
s = <<EOT The current value of variable bar is <%= bar %>. The current value of variable baz is <%= baz %>. The Ruby copyright is <%= RUBY_COPYRIGHT.inspect %>. The current process is <%= $0 %>.
Both of these calls raise NameError
, because bar
and baz
are not defined in either the default binding or the local binding.
puts ERB.new(s).result # Raises NameError. puts ERB.new(s).result(binding) # Raises NameError.
This call passes a hash that causes bar
and baz
to be defined in a new binding (derived from new_toplevel
):
hash = {bar: 3, baz: 4} # => {bar: 3, baz: 4} ERB.new(s).result_with_hash(hash) puts ERB.new(s).result_with_hash(variables) The current value of variable bar is 3. The current value of variable baz is 4. The Ruby copyright is "ruby - Copyright (C) 1993-2025 Yukihiro Matsumoto". The current process is irb. EOT
Tags¶ ↑
The examples above use expression tags. These are the tags available in ERB:
-
Expression tag: the tag contains a Ruby exprssion; in the result, the entire tag is to be replaced with the run-time value of the expression.
-
Execution tag: the tag contains Ruby code; in the result, the entire tag is to be replaced with the run-time value of the code.
-
Comment tag: the tag contains comment code; in the result, the entire tag is to be omitted.
Expression Tags¶ ↑
You can embed a Ruby expression in a template using an expression tag.
Its syntax is <%= expression %>
, where expression is any valid Ruby expression.
When you call method result
, the method evaluates the expression and replaces the entire expression tag with the expression’s value:
ERB.new('Today is <%= Date::DAYNAMES[Date.today.wday] %>.').result # => "Today is Monday." ERB.new('Tomorrow will be <%= Date::DAYNAMES[Date.today.wday + 1] %>.').result # => "Tomorrow will be Tuesday." ERB.new('Yesterday was <%= Date::DAYNAMES[Date.today.wday - 1] %>.').result # => "Yesterday was Sunday."
Note that whitespace before and after the expression is allowed but not required, and that such whitespace is stripped from the result.
ERB.new('My appointment is on <%=Date::DAYNAMES[Date.today.wday + 2]%>.').result # => "My appointment is on Wednesday." ERB.new('My appointment is on <%= Date::DAYNAMES[Date.today.wday + 2] %>.').result # => "My appointment is on Wednesday."
Execution Tags¶ ↑
You can embed Ruby executable code in template using an execution tag.
Its syntax is <% code %>
, where code is any valid Ruby code.
When you call method result
, the method executes the code and removes the entire execution tag (generating no text in the result):
ERB.new('foo <% Dir.chdir("C:/") %> bar').result # => "foo bar"
Whitespace before and after the embedded code is optional:
ERB.new('foo <%Dir.chdir("C:/")%> bar').result # => "foo bar"
You can interleave text with execution tags to form a control structure such as a conditional, a loop, or a case
statements.
Conditional:
s = <<EOT <% if verbosity %> An error has occurred. <% else %> Oops! <% end %> EOT template = ERB.new(s) verbosity = true template.result(binding) # => "\nAn error has occurred.\n\n" verbosity = false template.result(binding) # => "\nOops!\n\n"
Note that the interleaved text may itself contain expression tags:
Loop:
s = <<EOT <% Date::ABBR_DAYNAMES.each do |dayname| %> <%= dayname %> <% end %> EOT ERB.new(s).result # => "\nSun\n\nMon\n\nTue\n\nWed\n\nThu\n\nFri\n\nSat\n\n"
Other, non-control, lines of Ruby code may be interleaved with the text, and the Ruby code may itself contain regular Ruby comments:
s = <<EOT <% 3.times do %> <%= Time.now %> <% sleep(1) # Let's make the times different. %> <% end %> EOT ERB.new(s).result # => "\n2025-09-09 11:36:02 -0500\n\n\n2025-09-09 11:36:03 -0500\n\n\n2025-09-09 11:36:04 -0500\n\n\n"
The execution tag may also contain multiple lines of code:
s = <<EOT <% (0..2).each do |i| (0..2).each do |j| %> * <%=i%>,<%=j%> <% end end %> EOT ERB.new(s).result # => "\n* 0,0\n\n* 0,1\n\n* 0,2\n\n* 1,0\n\n* 1,1\n\n* 1,2\n\n* 2,0\n\n* 2,1\n\n* 2,2\n\n"
Shorthand Format for Execution Tags¶ ↑
You can use keyword argument trim_mode: '%'
to enable a shorthand format for execution tags; this example uses the shorthand format % code
instead of <% code %>
:
s = <<EOT % priorities.each do |priority| * <%= priority %> % end EOT template = ERB.new(s, trim_mode: '%') priorities = [ 'Run Ruby Quiz', 'Document Modules', 'Answer Questions on Ruby Talk' ] puts template.result(binding) * Run Ruby Quiz * Document Modules * Answer Questions on Ruby Talk
Note that in the shorthand format, the character '%'
must be the first character in the code line (no leading whitespace).
Suppressing Unwanted Blank Lines¶ ↑
With keyword argument trim_mode
not given, all blank lines go into the result:
s = <<EOT <% if true %> <%= RUBY_VERSION %> <% end %> EOT ERB.new(s).result.lines.each {|line| puts line.inspect } "\n" "3.4.5\n" "\n"
You can give trim_mode: '-'
, you can suppress each blank line whose source line ends with -%>
(instead of %>
):
s = <<EOT <% if true -%> <%= RUBY_VERSION %> <% end -%> EOT ERB.new(s, trim_mode: '-').result.lines.each {|line| puts line.inspect } "3.4.5\n"
It is an error to use the trailing '-%>'
notation without trim_mode: '-'
:
ERB.new(s).result.lines.each {|line| puts line.inspect } # Raises SyntaxError.
Suppressing Unwanted Newlines¶ ↑
Consider this input string:
s = <<EOT <% RUBY_VERSION %> <%= RUBY_VERSION %> foo <% RUBY_VERSION %> foo <%= RUBY_VERSION %> EOT
With keyword argument trim_mode
not given, all newlines go into the result:
ERB.new(s).result.lines.each {|line| puts line.inspect } "\n" "3.4.5\n" "foo \n" "foo 3.4.5\n"
You can give trim_mode: '>'
to suppress the trailing newline for each line that ends with '%<'
(regardless of its beginning):
ERB.new(s, trim_mode: '>').result.lines.each {|line| puts line.inspect } "3.4.5foo foo 3.4.5"
You can give trim_mode: '<>'
to suppress the trailing newline for each line that both begins with '<%'
and ends with '%>'
:
ERB.new(s, trim_mode: '<>').result.lines.each {|line| puts line.inspect } "3.4.5foo \n" "foo 3.4.5\n"
Combining Trim Modes¶ ↑
You can combine certain trim modes:
-
'%-'
: Enable shorthand and omit each blank line ending with'-%>'
. -
'%>'
: Enable shorthand and omit newline for each line ending with'%>'
. -
'%<>'
: Enable shorthand and omit newline for each line starting with'<%'
and ending with'%>'
.
Comment Tags¶ ↑
You can embed a comment in a template using a comment tag; its syntax is <%# text %>
, where text is the text of the comment.
When you call method result
, it removes the entire comment tag (generating no text in the result).
Example:
s = 'Some stuff;<%# Note to self: figure out what the stuff is. %> more stuff.' ERB.new(s).result # => "Some stuff; more stuff."
A comment tag may appear anywhere in the template text.
Note that the beginning of the tag must be '<%#'
, not '<% #'
.
In this example, the tag begins with '<% #'
, and so is an execution tag, not a comment tag; the cited code consists entirely of a Ruby-style comment (which is of course ignored):
ERB.new('Some stuff;<% # Note to self: figure out what the stuff is. %> more stuff.').result # => "Some stuff;"
Encodings¶ ↑
An ERB template has an encoding, which is by default the encoding of the source string; the result string will also have that encoding.
s = <<EOT <%# Comment. %> EOT template = ERB.new(s) s.encoding # => #<Encoding:UTF-8> template.encoding # => #<Encoding:UTF-8> template.result.encoding # => #<Encoding:UTF-8>
You can specify a different encoding by adding a magic comment at the top of the given string:
s = <<EOT <%#-*- coding: Big5 -*-%> <%# Comment. %> EOT template = ERB.new(s) s.encoding # => #<Encoding:UTF-8> template.encoding # => #<Encoding:Big5> template.result.encoding # => #<Encoding:Big5>
Error Reporting¶ ↑
Consider this template (containing an error):
s = '<%= nosuch %>' template = ERB.new(s)
When ERB reports an error, it includes a file name (if available) and a line number; the file name comes from method filename
, the line number from method lineno
.
Initially, those values are nil
and 0
, respectively; these initial values are reported as '(erb)'
and 1
, respectively:
template.filename # => nil template.lineno # => 0 template.result (erb):1:in '<main>': undefined local variable or method 'nosuch' for main (NameError)
You can use methods filename=
and lineno=
to assign values that are more meaningful in your context:
template.filename = 't.txt' # => "t.txt" template.lineno = 555 # => 555 template.result t.txt:556:in '<main>': undefined local variable or method 'nosuch' for main (NameError)
You can use method location=
to set both values:
template.location = ['u.txt', 999] template.result u.txt:1000:in '<main>': undefined local variable or method 'nosuch' for main (NameError)
Plain Text Example¶ ↑
Here’s a plain-text string; it uses the literal notation '%q{ ... }'
to define the string (see %q literals); this avoids problems with backslashes.
s = %q{ From: James Edward Gray II <james@grayproductions.net> To: <%= to %> Subject: Addressing Needs <%= to[/\w+/] %>: Just wanted to send a quick note assuring that your needs are being addressed. I want you to know that my team will keep working on the issues, especially: <%# ignore numerous minor requests -- focus on priorities %> % priorities.each do |priority| * <%= priority %> % end Thanks for your patience. James Edward Gray II }
The template will need these:
to = 'Community Spokesman <spokesman@ruby_community.org>' priorities = [ 'Run Ruby Quiz', 'Document Modules', 'Answer Questions on Ruby Talk' ]
Finally, make the template and get the result
template = ERB.new(s, trim_mode: '%<>') puts template.result(binding) From: James Edward Gray II <james@grayproductions.net> To: Community Spokesman <spokesman@ruby_community.org> Subject: Addressing Needs Community: Just wanted to send a quick note assuring that your needs are being addressed. I want you to know that my team will keep working on the issues, especially: * Run Ruby Quiz * Document Modules * Answer Questions on Ruby Talk Thanks for your patience. James Edward Gray II
HTML Example¶ ↑
This example shows an HTML template.
First, here’s a custom class, Product
:
class Product def initialize(code, name, desc, cost) @code = code @name = name @desc = desc @cost = cost @features = [] end def add_feature(feature) @features << feature end # Support templating of member data. def get_binding binding end end
The template below will need these values:
toy = Product.new('TZ-1002', 'Rubysapien', "Geek's Best Friend! Responds to Ruby commands...", 999.95 ) toy.add_feature('Listens for verbal commands in the Ruby language!') toy.add_feature('Ignores Perl, Java, and all C variants.') toy.add_feature('Karate-Chop Action!!!') toy.add_feature('Matz signature on left leg.') toy.add_feature('Gem studded eyes... Rubies, of course!')
Here’s the HTML:
s = <<EOT <html> <head><title>Ruby Toys -- <%= @name %></title></head> <body> <h1><%= @name %> (<%= @code %>)</h1> <p><%= @desc %></p> <ul> <% @features.each do |f| %> <li><b><%= f %></b></li> <% end %> </ul> <p> <% if @cost < 10 %> <b>Only <%= @cost %>!!!</b> <% else %> Call for a price, today! <% end %> </p> </body> </html> EOT
Finally, build the template and get the result (omitting some blank lines):
template = ERB.new(s) puts template.result(toy.get_binding) <html> <head><title>Ruby Toys -- Rubysapien</title></head> <body> <h1>Rubysapien (TZ-1002)</h1> <p>Geek's Best Friend! Responds to Ruby commands...</p> <ul> <li><b>Listens for verbal commands in the Ruby language!</b></li> <li><b>Ignores Perl, Java, and all C variants.</b></li> <li><b>Karate-Chop Action!!!</b></li> <li><b>Matz signature on left leg.</b></li> <li><b>Gem studded eyes... Rubies, of course!</b></li> </ul> <p> Call for a price, today! </p> </body> </html>
Other Template Processors¶ ↑
Various Ruby projects have their own template processors. The Ruby Processing System RDoc, for example, has one that can be used elsewhere.
Other popular template processors may found in the Template Engines page of the Ruby Toolbox.
Constants
- NOT_GIVEN
-
Placeholder constant; used as default value for certain method arguments.
- VERSION
-
The string ERB version.
Attributes
Returns the encoding of self
; see Encodings:
Sets or returns the file name to be used in reporting errors; see Error Reporting.
Sets or returns the line number to be used in reporting errors; see Error Reporting.
Returns a string containing the Ruby code that, when executed, generates the result; the code is executed by method result
, and by its wrapper methods result_with_hash
and run
:
s = 'The time is <%= Time.now %>.' template = ERB.new(s) template.src # => "#coding:UTF-8\n_erbout = +''; _erbout.<< \"The time is \".freeze; _erbout.<<(( Time.now ).to_s); _erbout.<< \".\".freeze; _erbout" template.result # => "The time is 2025-09-18 15:58:08 -0500."
In a more readable format:
# puts template.src.split('; ') # #coding:UTF-8 # _erbout = +'' # _erbout.<< "The time is ".freeze # _erbout.<<(( Time.now ).to_s) # _erbout.<< ".".freeze # _erbout
Variable _erbout
is used to store the intermediate results in the code; the name _erbout
is the default in ERB.new
, and can be changed via keyword argument eoutvar
:
template = ERB.new(s, eoutvar: '_foo') puts template.src.split('; ') #coding:UTF-8 _foo = +'' _foo.<< "The time is ".freeze _foo.<<(( Time.now ).to_s) _foo.<< ".".freeze _foo
Public Class Methods
Source
# File lib/erb.rb, line 846 def initialize(str, safe_level=NOT_GIVEN, legacy_trim_mode=NOT_GIVEN, legacy_eoutvar=NOT_GIVEN, trim_mode: nil, eoutvar: '_erbout') # Complex initializer for $SAFE deprecation at [Feature #14256]. Use keyword arguments to pass trim_mode or eoutvar. if safe_level != NOT_GIVEN warn 'Passing safe_level with the 2nd argument of ERB.new is deprecated. Do not use it, and specify other arguments as keyword arguments.', uplevel: 1 end if legacy_trim_mode != NOT_GIVEN warn 'Passing trim_mode with the 3rd argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, trim_mode: ...) instead.', uplevel: 1 trim_mode = legacy_trim_mode end if legacy_eoutvar != NOT_GIVEN warn 'Passing eoutvar with the 4th argument of ERB.new is deprecated. Use keyword argument like ERB.new(str, eoutvar: ...) instead.', uplevel: 1 eoutvar = legacy_eoutvar end compiler = make_compiler(trim_mode) set_eoutvar(compiler, eoutvar) @src, @encoding, @frozen_string = *compiler.compile(str) @filename = nil @lineno = 0 @_init = self.class.singleton_class end
Returns a new ERB object containing the given string
.
For details about string
, its embedded tags, and generated results, see ERB
.
Keyword Argument trim_mode
You can use keyword argument trim_mode: '%'
to enable the shorthand format for execution tags.
This value allows blank line control:
-
'-'
: Omit each blank line ending with'%>'
.
Other values allow newline control:
-
'>'
: Omit newline for each line ending with'%>'
. -
'<>'
: Omit newline for each line starting with'<%'
and ending with'%>'
.
You can also combine trim modes.
Keyword Argument eoutvar
The string value of keyword argument eoutvar
specifies the name of the variable that method result
uses to construct its result string; see src
.
This is useful when you need to run multiple ERB templates through the same binding and/or when you want to control where output ends up.
It’s good practice to choose a variable name that begins with an underscore: '_'
.
Backward Compatibility
The calling sequence given above – which is the one you should use – is a simplified version of the complete formal calling sequence, which is:
ERB.new(string, safe_level=NOT_GIVEN, legacy_trim_mode=NOT_GIVEN, legacy_eoutvar=NOT_GIVEN, trim_mode: nil, eoutvar: '_erbout')
The second, third, and fourth positional arguments (those in the second line above) are deprecated; this method issues warnings if they are given.
However, their values, if given, are handled thus:
-
safe_level
: ignored. -
legacy_trim_mode
: overrides keyword argumenttrim_mode
. -
legacy_eoutvar
: overrides keyword argumenteoutvar
.
Source
# File lib/erb.rb, line 780 def self.version VERSION end
Returns the string ERB version.
Public Instance Methods
Source
# File lib/erb.rb, line 1205 def def_class(superklass=Object, methodname='result') cls = Class.new(superklass) def_method(cls, methodname, @filename || '(ERB)') cls end
Returns a new nameless class whose superclass is super_class
, and which has instance method method_name
.
Create a template from HTML that has embedded expression tags that use @arg1
and @arg2
:
html = <<EOT <html> <body> <p><%= @arg1 %></p> <p><%= @arg2 %></p> </body> </html> EOT template = ERB.new(html)
Create a base class that has @arg1
and @arg2
:
class MyBaseClass def initialize(arg1, arg2) @arg1 = arg1 @arg2 = arg2 end end
Use method def_class
to create a subclass that has method :render
:
MySubClass = template.def_class(MyBaseClass, :render)
Generate the result:
puts MySubClass.new('foo', 123).render <html> <body> <p>foo</p> <p>123</p> </body> </html>
Source
# File lib/erb.rb, line 1122 def def_method(mod, methodname, fname='(ERB)') src = self.src.sub(/^(?!#|$)/) {"def #{methodname}\n"} << "\nend\n" mod.module_eval do eval(src, binding, fname, -1) end end
Creates and returns a new instance method in the given module module
; returns the method name as a symbol.
The method is created from the given method_signature
, which consists of the method name and its argument names (if any).
The filename
sets the value of filename
; see Error Reporting.
s = '<%= arg1 %> <%= arg2 %>' template = ERB.new(s) MyModule = Module.new template.def_method(MyModule, 'render(arg1, arg2)') # => :render class MyClass; include MyModule; end MyClass.new.render('foo', 123) # => "foo 123"
Source
# File lib/erb.rb, line 1147 def def_module(methodname='erb') mod = Module.new def_method(mod, methodname, @filename || '(ERB)') mod end
Returns a new nameless module that has instance method method_name
.
s = '<%= arg1 %> <%= arg2 %>' template = ERB.new(s) MyModule = template.def_module('render(arg1, arg2)') class MyClass include MyModule end MyClass.new.render('foo', 123) # => "foo 123"
Source
# File lib/erb.rb, line 971 def location=((filename, lineno)) @filename = filename @lineno = lineno if lineno end
Sets the values of filename
and, if given, lineno
; see Error Reporting.
Source
# File lib/erb.rb, line 888 def make_compiler(trim_mode) ERB::Compiler.new(trim_mode) end
Returns a new ERB::Compiler
with the given trim_mode
; for trim_mode
values, see ERB.new
:
ERB.new('').make_compiler(nil) # => #<ERB::Compiler:0x000001cff9467678 @insert_cmd="print", @percent=false, @post_cmd=[], @pre_cmd=[], @put_cmd="print", @trim_mode=nil>
Source
# File lib/erb.rb, line 1042 def result(b=new_toplevel) unless @_init.equal?(self.class.singleton_class) raise ArgumentError, "not initialized" end eval(@src, b, (@filename || '(erb)'), @lineno) end
Returns the new string formed by processing ERB tags found in the stored string in self
.
With no argument given, uses the default binding; see Default Binding.
With argument binding
given, uses the local binding; see Local Binding.
See also result_with_hash
.
Source
# File lib/erb.rb, line 1061 def result_with_hash(hash) b = new_toplevel(hash.keys) hash.each_pair do |key, value| b.local_variable_set(key, value) end result(b) end
Returns the new string formed by processing ERB tags found in the stored string in self
; see Augmented Binding.
See also result
.
Source
# File lib/erb.rb, line 1020 def run(b=new_toplevel) print self.result(b) end
Like result
, but prints the result string (instead of returning it); returns nil
.
Source
# File lib/erb.rb, line 1006 def set_eoutvar(compiler, eoutvar = '_erbout') compiler.put_cmd = "#{eoutvar}.<<" compiler.insert_cmd = "#{eoutvar}.<<" compiler.pre_cmd = ["#{eoutvar} = +''"] compiler.post_cmd = [eoutvar] end
Sets the eoutvar
value in the ERB::Compiler
object compiler
; returns a 1-element array containing the value of eoutvar
:
template = ERB.new('') compiler = template.make_compiler(nil) pp compiler #<ERB::Compiler:0x000001cff8a9aa00 @insert_cmd="print", @percent=false, @post_cmd=[], @pre_cmd=[], @put_cmd="print", @trim_mode=nil> template.set_eoutvar(compiler, '_foo') # => ["_foo"] pp compiler #<ERB::Compiler:0x000001cff8a9aa00 @insert_cmd="_foo.<<", @percent=false, @post_cmd=["_foo"], @pre_cmd=["_foo = +''"], @put_cmd="_foo.<<", @trim_mode=nil>
Private Instance Methods
Source
# File lib/erb.rb, line 1085 def new_toplevel(vars = nil) b = TOPLEVEL_BINDING if vars vars = vars.select {|v| b.local_variable_defined?(v)} unless vars.empty? return b.eval("tap {|;#{vars.join(',')}| break binding}") end end b.dup end
Returns a new binding based on TOPLEVEL_BINDING
; used to create a default binding for a call to result
.
See Default Binding.
Argument symbols
is an array of symbols; each symbol symbol
is defined as a new variable to hide and prevent it from overwriting a variable of the same name already defined within the binding.