class ERB
Class ERB (名称代表 Embedded Ruby) 是一个易于使用但功能也非常强大的 模板处理器。
用法
在使用 ERB 之前,必须先 require 它(本页面的示例假设已完成此操作)。
require 'erb'
简而言之
ERB 的工作原理如下:
-
您可以创建一个模板:一个包含特殊格式标签的纯文本字符串。
-
您可以创建一个 ERB 对象来存储模板。
-
您可以调用实例方法
ERB#result来获取结果。
ERB 支持三种类型的标签:
-
表达式标签:每个以
'<%='开始,以'%>'结束;包含一个 Ruby 表达式;在结果中,表达式的值将替换整个标签。template = 'The magic word is <%= magic_word %>.' erb = ERB.new(template) magic_word = 'xyzzy' erb.result(binding) # => "The magic word is xyzzy."
上面对
result的调用传递了参数binding,该参数包含变量magic_word到其字符串值'xyzzy'的绑定。下面对
result的调用不需要传递 binding,因为它的表达式Date::DAYNAMES是全局定义的。ERB.new('Today is <%= Date::DAYNAMES[Date.today.wday] %>.').result # => "Today is Monday."
-
执行标签:每个以
'<%'开始,以'%>'结束;包含要执行的 Ruby 代码。template = '<% File.write("t.txt", "Some stuff.") %>' ERB.new(template).result File.read('t.txt') # => "Some stuff."
-
注释标签:每个以
'<%#'开始,以'%>'结束;包含注释文本;在结果中,整个标签将被省略。template = 'Some stuff;<%# Note to self: figure out what the stuff is. %> more stuff.' ERB.new(template).result # => "Some stuff; more stuff."
一些简单的例子
这是 ERB 运行的一个简单示例:
template = 'The time is <%= Time.now %>.' erb = ERB.new(template) erb.result # => "The time is 2025-09-09 10:49:26 -0500."
详情
-
一个纯文本字符串被赋给变量
template。其嵌入的 表达式标签'<%= Time.now %>'包含一个 Ruby 表达式Time.now。 -
该字符串被放入一个新的 ERB 对象中,并存储在变量
erb中。 -
方法调用
erb.result生成一个包含Time.now运行时值的字符串,该值在调用时计算。
ERB 对象可以被重用。
erb.result # => "The time is 2025-09-09 10:49:33 -0500."
另一个例子
template = 'The magic word is <%= magic_word %>.' erb = ERB.new(template) magic_word = 'abracadabra' erb.result(binding) # => "The magic word is abracadabra."
详情
-
和之前一样,一个纯文本字符串被赋给变量
template。其嵌入的 表达式标签'<%= magic_word %>'包含一个变量名称,即magic_word。 -
该字符串被放入一个新的 ERB 对象中,并存储在变量
erb中;请注意,magic_word在创建 ERB 对象之前不必定义。 -
magic_word = 'abracadabra'为变量magic_word赋一个值。 -
方法调用
erb.result(binding)生成一个包含magic_word值的字符串。
和之前一样,ERB 对象可以被重用。
magic_word = 'xyzzy' erb.result(binding) # => "The magic word is xyzzy."
Binding(绑定)
调用方法 result(该方法生成格式化的结果字符串)需要一个 Binding 对象作为参数。
binding 对象为 表达式标签中的表达式提供绑定。
提供必需的 binding 有三种方式:
默认 Binding
当您将 no binding 参数传递给方法 result 时,该方法使用其默认绑定:由方法 new_toplevel 返回的那个。此绑定包含 Ruby 本身定义的绑定,即 Ruby 的常量和变量的绑定。
该绑定足以满足仅引用 Ruby 的常量和变量的表达式标签;这些表达式标签仅引用 Ruby 的全局常量 RUBY_COPYRIGHT 和全局变量 $0。
template = <<TEMPLATE The Ruby copyright is <%= RUBY_COPYRIGHT.inspect %>. The current process is <%= $0 %>. TEMPLATE puts ERB.new(template).result The Ruby copyright is "ruby - Copyright (C) 1993-2025 Yukihiro Matsumoto". The current process is irb.
(当前进程是 irb,因为我们在这里做这些示例!)
局部 Binding
默认绑定不足以满足引用不在该处定义的常量或变量的表达式。
Foo = 1 # Defines local constant Foo. foo = 2 # Defines local variable foo. template = <<TEMPLATE 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 %>. TEMPLATE erb = ERB.new(template)
下面的调用将引发 NameError,因为尽管 Foo 和 foo 是局部定义的,但在默认绑定中它们并未定义。
erb.result # Raises NameError.
为了使局部定义的常量和变量可用,您可以调用 result 并传入局部 binding。
puts erb.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.
增强 Binding
另一种使变量绑定(但不是常量绑定)可用的方法是使用方法 result_with_hash(hash);传递的哈希包含名称/值对,这些对将用于在默认绑定的副本中定义和赋值变量。
template = <<TEMPLATE 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 %>. TEMPLATE erb = ERB.new(template)
这两个调用都会引发 NameError,因为 bar 和 baz 在默认绑定或局部绑定中均未定义。
puts erb.result # Raises NameError. puts erb.result(binding) # Raises NameError.
此调用传递一个哈希,该哈希导致 bar 和 baz 在新的绑定(派生自 new_toplevel)中定义。
hash = {bar: 3, baz: 4}
puts erb.result_with_hash(hash)
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.
标签
上面的示例使用了表达式标签。以下是 ERB 中可用的标签:
-
表达式标签:标签包含一个 Ruby 表达式;在结果中,整个标签将被替换为表达式的运行时值。
-
执行标签:标签包含 Ruby 代码;在结果中,整个标签将被替换为代码的运行时值。
-
注释标签:标签包含注释代码;在结果中,整个标签将被省略。
表达式标签
您可以使用表达式标签将 Ruby 表达式嵌入到模板中。
其语法是 <%= expression %>,其中 expression 是任何有效的 Ruby 表达式。
当您调用方法 result 时,该方法将评估表达式,并用表达式的值替换整个表达式标签。
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."
请注意,表达式前后允许有空格,但不是必需的,并且这些空格将从结果中去除。
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."
执行标签
您可以使用执行标签将 Ruby 可执行代码嵌入到模板中。
其语法是 <% code %>,其中 code 是任何有效的 Ruby 代码。
当您调用方法 result 时,该方法将执行代码并移除整个执行标签(在结果中不生成任何文本)。
ERB.new('foo <% Dir.chdir("C:/") %> bar').result # => "foo bar"
嵌入代码前后允许有空格。
ERB.new('foo <%Dir.chdir("C:/")%> bar').result # => "foo bar"
您可以将文本与执行标签交错,形成如条件语句、循环或 case 语句等控制结构。
条件语句
template = <<TEMPLATE <% if verbosity %> An error has occurred. <% else %> Oops! <% end %> TEMPLATE erb = ERB.new(template) verbosity = true erb.result(binding) # => "\nAn error has occurred.\n\n" verbosity = false erb.result(binding) # => "\nOops!\n\n"
请注意,交错的文本本身也可以包含表达式标签。
循环
template = <<TEMPLATE <% Date::ABBR_DAYNAMES.each do |dayname| %> <%= dayname %> <% end %> TEMPLATE ERB.new(template).result # => "\nSun\n\nMon\n\nTue\n\nWed\n\nThu\n\nFri\n\nSat\n\n"
其他非控制行的 Ruby 代码可以与文本交错,并且 Ruby 代码本身可以包含常规的 Ruby 注释。
template = <<TEMPLATE <% 3.times do %> <%= Time.now %> <% sleep(1) # Let's make the times different. %> <% end %> TEMPLATE ERB.new(template).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"
执行标签也可以包含多行代码。
template = <<TEMPLATE <% (0..2).each do |i| (0..2).each do |j| %> * <%=i%>,<%=j%> <% end end %> TEMPLATE ERB.new(template).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"
执行标签的简写格式
您可以使用关键字参数 trim_mode: '%' 来启用执行标签的简写格式;此示例使用简写格式 % code 而不是 <% code %>。
template = <<TEMPLATE
% priorities.each do |priority|
* <%= priority %>
% end
TEMPLATE
erb = ERB.new(template, trim_mode: '%')
priorities = [ 'Run Ruby Quiz',
'Document Modules',
'Answer Questions on Ruby Talk' ]
puts erb.result(binding)
* Run Ruby Quiz
* Document Modules
* Answer Questions on Ruby Talk
请注意,在简写格式中,字符 '%' 必须是代码行的第一个字符(不允许有前导空格)。
抑制不需要的空白行
当未给出关键字参数 trim_mode 时,所有空白行都会进入结果。
template = <<TEMPLATE <% if true %> <%= RUBY_VERSION %> <% end %> TEMPLATE ERB.new(template).result.lines.each {|line| puts line.inspect } "\n" "3.4.5\n" "\n"
您可以指定 trim_mode: '-',可以抑制源行以 -%>(而不是 %>)结尾的每个空白行。
template = <<TEMPLATE <% if true -%> <%= RUBY_VERSION %> <% end -%> TEMPLATE ERB.new(template, trim_mode: '-').result.lines.each {|line| puts line.inspect } "3.4.5\n"
使用尾部 '-%>' 符号但不使用 trim_mode: '-' 是错误的。
ERB.new(template).result.lines.each {|line| puts line.inspect } # Raises SyntaxError.
抑制不需要的换行符
考虑这个模板:
template = <<TEMPLATE <% RUBY_VERSION %> <%= RUBY_VERSION %> foo <% RUBY_VERSION %> foo <%= RUBY_VERSION %> TEMPLATE
当未给出关键字参数 trim_mode 时,所有换行符都会进入结果。
ERB.new(template).result.lines.each {|line| puts line.inspect } "\n" "3.4.5\n" "foo \n" "foo 3.4.5\n"
您可以指定 trim_mode: '>' 来抑制每行以 '%>' 结尾的尾部换行符(无论其开头是什么)。
ERB.new(template, trim_mode: '>').result.lines.each {|line| puts line.inspect } "3.4.5foo foo 3.4.5"
您可以指定 trim_mode: '<>' 来抑制每行同时以 '<%' 开始并以 '%>' 结束的尾部换行符。
ERB.new(template, trim_mode: '<>').result.lines.each {|line| puts line.inspect } "3.4.5foo \n" "foo 3.4.5\n"
合并修剪模式
您可以组合某些修剪模式。
-
'%-':启用简写并省略以'-%>'结尾的每个空白行。 -
'%>':启用简写并省略以'%>'结尾的每行的换行符。 -
'%<>':启用简写并省略以'<%'开始并以'%>'结束的每行的换行符。
注释标签
您可以使用注释标签将注释嵌入到模板中;其语法是 <%# text %>,其中 text 是注释的文本。
当您调用方法 result 时,它会移除整个注释标签(在结果中不生成任何文本)。
示例
template = 'Some stuff;<%# Note to self: figure out what the stuff is. %> more stuff.' ERB.new(template).result # => "Some stuff; more stuff."
注释标签可以出现在模板的任何位置。
请注意,标签的开头必须是 '<%#',而不是 '<% #'。
在此示例中,标签以 '<% #' 开头,因此是一个执行标签,而不是注释标签;引用的代码完全由一个 Ruby 风格的注释组成(当然会被忽略)。
ERB.new('Some stuff;<% # Note to self: figure out what the stuff is. %> more stuff.').result # => "Some stuff;"
编码
ERB 对象有一个编码,默认情况下是模板字符串的编码;结果字符串也将具有该编码。
template = <<TEMPLATE <%# Comment. %> TEMPLATE erb = ERB.new(template) template.encoding # => #<Encoding:UTF-8> erb.encoding # => #<Encoding:UTF-8> erb.result.encoding # => #<Encoding:UTF-8>
您可以通过在给定模板的顶部添加魔术注释来指定不同的编码。
template = <<TEMPLATE <%#-*- coding: Big5 -*-%> <%# Comment. %> TEMPLATE erb = ERB.new(template) template.encoding # => #<Encoding:UTF-8> erb.encoding # => #<Encoding:Big5> erb.result.encoding # => #<Encoding:Big5>
错误报告
考虑这个模板(包含错误):
template = '<%= nosuch %>' erb = ERB.new(template)
当 ERB 报告错误时,它会包含一个文件名(如果可用)和一个行号;文件名来自方法 filename,行号来自方法 lineno。
最初,这些值分别是 nil 和 0;这些初始值分别报告为 '(erb)' 和 1。
erb.filename # => nil erb.lineno # => 0 erb.result (erb):1:in '<main>': undefined local variable or method 'nosuch' for main (NameError)
您可以使用方法 filename= 和 lineno= 来分配对您的上下文更有意义的值。
erb.filename = 't.txt' erb.lineno = 555 erb.result t.txt:556:in '<main>': undefined local variable or method 'nosuch' for main (NameError)
您可以使用方法 location= 来同时设置这两个值。
erb.location = ['u.txt', 999] erb.result u.txt:1000:in '<main>': undefined local variable or method 'nosuch' for main (NameError)
带嵌入 Ruby 的纯文本
这是一个纯文本模板;它使用字面量表示法 '%q{ ... }' 来定义模板(请参阅 %q 字面量);这避免了反斜杠问题。
template = %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 }
模板将需要这些:
to = 'Community Spokesman <spokesman@ruby_community.org>' priorities = [ 'Run Ruby Quiz', 'Document Modules', 'Answer Questions on Ruby Talk' ]
最后,创建 ERB 对象并获取结果。
erb = ERB.new(template, trim_mode: '%<>') puts erb.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
带嵌入 Ruby 的 HTML
此示例显示了一个 HTML 模板。
首先,这是一个自定义类 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
下面的模板将需要这些值:
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!')
这是 HTML:
template = <<TEMPLATE <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> TEMPLATE
最后,创建 ERB 对象并获取结果(省略一些空白行)。
erb = ERB.new(template)
puts erb.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>
其他模板处理器
各种 Ruby 项目都有自己的模板处理器。例如,Ruby 处理系统 RDoc 有一个可以在其他地方使用的处理器。
您可以在 Ruby Toolbox 的模板引擎页面上找到其他流行的模板处理器。
Constants
- VERSION
-
字符串 ERB 版本。
属性
返回 self 的编码;请参阅 Encodings。
设置或返回用于报告错误的文件名;请参阅 Error Reporting。
设置或返回用于报告错误的行号;请参阅 Error Reporting。
返回执行后可生成结果的 Ruby 代码;该代码由方法 result 及其包装方法 result_with_hash 和 run 执行。
template = 'The time is <%= Time.now %>.' erb = ERB.new(template) erb.src # => "#coding:UTF-8\n_erbout = +''; _erbout.<< \"The time is \".freeze; _erbout.<<(( Time.now ).to_s); _erbout.<< \".\".freeze; _erbout" erb.result # => "The time is 2025-09-18 15:58:08 -0500."
以更易读的格式
# puts erb.src.split('; ') # #coding:UTF-8 # _erbout = +'' # _erbout.<< "The time is ".freeze # _erbout.<<(( Time.now ).to_s) # _erbout.<< ".".freeze # _erbout
变量 _erbout 用于存储代码中的中间结果;_erbout 的名称是 ERB.new 中的默认名称,可以通过关键字参数 eoutvar 进行更改。
erb = ERB.new(template, 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 831 def initialize(str, trim_mode: nil, eoutvar: '_erbout') 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
返回一个包含给定字符串 template 的新 ERB 对象。
有关 template、其嵌入的标签和生成结果的详细信息,请参阅 ERB。
关键字参数 trim_mode
您可以使用关键字参数 trim_mode: '%' 来启用执行标签的简写格式。
此值允许空白行控制。
-
'-':省略以'%>'结尾的每个空白行。
其他值允许换行符控制。
-
'>':省略以'%>'结尾的每行的换行符。 -
'<>':省略以'<%'开始并以'%>'结尾的每行的换行符。
您也可以组合修剪模式。
关键字参数 eoutvar
关键字参数 eoutvar 的字符串值指定了方法 result 用于构建其结果字符串的变量的名称;请参阅 src。
当您需要通过相同的 binding 运行多个 ERB 模板以及/或您想控制输出去向时,这很有用。
建议选择一个以下划线 '_' 开头的变量名。
Public Instance Methods
Source
# File lib/erb.rb, line 1169 def def_class(superklass=Object, methodname='result') cls = Class.new(superklass) def_method(cls, methodname, @filename || '(ERB)') cls end
返回一个没有名称的新类,其超类是 super_class,并具有实例方法 method_name。
从包含嵌入表达式标签使用 @arg1 和 @arg2 的 HTML 创建一个模板。
html = <<TEMPLATE <html> <body> <p><%= @arg1 %></p> <p><%= @arg2 %></p> </body> </html> TEMPLATE template = ERB.new(html)
创建一个具有 @arg1 和 @arg2 的基类。
class MyBaseClass def initialize(arg1, arg2) @arg1 = arg1 @arg2 = arg2 end end
使用方法 def_class 创建一个具有 :render 方法的子类。
MySubClass = template.def_class(MyBaseClass, :render)
生成结果。
puts MySubClass.new('foo', 123).render
<html>
<body>
<p>foo</p>
<p>123</p>
</body>
</html>
Source
# File lib/erb.rb, line 1087 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
在给定的模块 module 中创建并返回一个新的实例方法;返回方法名(符号)。
该方法由给定的 method_signature 创建,该签名由方法名及其参数名(如果有)组成。
filename 设置 filename 的值;请参阅 Error Reporting。
template = '<%= arg1 %> <%= arg2 %>' erb = ERB.new(template) MyModule = Module.new erb.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 1112 def def_module(methodname='erb') mod = Module.new def_method(mod, methodname, @filename || '(ERB)') mod end
返回一个没有名称的新模块,该模块具有实例方法 method_name。
template = '<%= arg1 %> <%= arg2 %>' erb = ERB.new(template) 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 936 def location=((filename, lineno)) @filename = filename @lineno = lineno if lineno end
设置 filename 的值,以及(如果给定)lineno 的值;请参阅 Error Reporting。
Source
# File lib/erb.rb, line 853 def make_compiler(trim_mode) ERB::Compiler.new(trim_mode) end
返回一个新的 ERB::Compiler,并带有给定的 trim_mode;有关 trim_mode 的值,请参阅 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 1007 def result(b=new_toplevel) unless @_init.equal?(self.class.singleton_class) raise ArgumentError, "not initialized" end eval(@src, b, (@filename || '(erb)'), @lineno) end
返回通过处理 self 中存储的模板中的 ERB 标签形成的字符串结果。
如果不给出参数,则使用默认绑定;请参阅 Default Binding。
给出参数 binding 时,则使用局部绑定;请参阅 Local Binding。
另请参阅 result_with_hash。
Source
# File lib/erb.rb, line 1026 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
返回通过处理 self 中存储的字符串中的 ERB 标签形成的字符串结果;请参阅 Augmented Binding。
另请参阅 result。
Source
# File lib/erb.rb, line 985 def run(b=new_toplevel) print self.result(b) end
与 result 类似,但将结果字符串打印出来(而不是返回它);返回 nil。
Source
# File lib/erb.rb, line 971 def set_eoutvar(compiler, eoutvar = '_erbout') compiler.put_cmd = "#{eoutvar}.<<" compiler.insert_cmd = "#{eoutvar}.<<" compiler.pre_cmd = ["#{eoutvar} = +''"] compiler.post_cmd = [eoutvar] end
在 ERB::Compiler 对象 compiler 中设置 eoutvar 的值;返回一个包含 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>
私有实例方法
Source
# File lib/erb.rb, line 1050 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
返回一个基于 TOPLEVEL_BINDING 的新 binding;用于为调用 result 创建默认 binding。
请参阅 Default Binding。
参数 symbols 是一个符号数组;每个符号 symbol 被定义为一个新变量,以隐藏并防止它覆盖 binding 中已存在的同名变量。