rbs 3.9.5 → 3.10.0.pre.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.clang-format +74 -0
- data/.clangd +2 -0
- data/.github/workflows/c-check.yml +54 -0
- data/.github/workflows/comments.yml +3 -3
- data/.github/workflows/ruby.yml +34 -19
- data/.github/workflows/typecheck.yml +1 -1
- data/.github/workflows/windows.yml +1 -1
- data/.gitignore +4 -0
- data/README.md +38 -1
- data/Rakefile +152 -23
- data/config.yml +190 -62
- data/core/array.rbs +44 -43
- data/core/dir.rbs +2 -2
- data/core/encoding.rbs +3 -2
- data/core/enumerable.rbs +89 -2
- data/core/errno.rbs +8 -0
- data/core/errors.rbs +28 -1
- data/core/exception.rbs +2 -2
- data/core/fiber.rbs +3 -3
- data/core/file.rbs +26 -11
- data/core/float.rbs +1 -1
- data/core/gc.rbs +422 -281
- data/core/hash.rbs +1024 -727
- data/core/io/wait.rbs +11 -33
- data/core/io.rbs +6 -4
- data/core/kernel.rbs +49 -43
- data/core/marshal.rbs +1 -1
- data/core/match_data.rbs +1 -1
- data/core/math.rbs +42 -3
- data/core/method.rbs +14 -6
- data/core/module.rbs +71 -11
- data/core/nil_class.rbs +3 -3
- data/core/numeric.rbs +8 -8
- data/core/object.rbs +3 -3
- data/core/object_space.rbs +13 -0
- data/{stdlib/pathname/0 → core}/pathname.rbs +253 -352
- data/core/proc.rbs +15 -8
- data/core/process.rbs +2 -2
- data/core/ractor.rbs +278 -437
- data/core/range.rbs +6 -7
- data/core/rbs/unnamed/argf.rbs +1 -1
- data/core/rbs/unnamed/env_class.rbs +1 -1
- data/core/rbs/unnamed/random.rbs +4 -2
- data/core/regexp.rbs +22 -17
- data/core/ruby_vm.rbs +6 -4
- data/core/rubygems/errors.rbs +3 -70
- data/core/rubygems/rubygems.rbs +11 -79
- data/core/set.rbs +439 -332
- data/core/string.rbs +2897 -1117
- data/core/struct.rbs +1 -1
- data/core/symbol.rbs +4 -4
- data/core/thread.rbs +83 -20
- data/core/time.rbs +35 -9
- data/core/unbound_method.rbs +14 -6
- data/docs/aliases.md +79 -0
- data/docs/collection.md +2 -2
- data/docs/gem.md +0 -1
- data/docs/sigs.md +3 -3
- data/ext/rbs_extension/ast_translation.c +1016 -0
- data/ext/rbs_extension/ast_translation.h +37 -0
- data/ext/rbs_extension/class_constants.c +157 -0
- data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +7 -1
- data/ext/rbs_extension/compat.h +10 -0
- data/ext/rbs_extension/extconf.rb +25 -1
- data/ext/rbs_extension/legacy_location.c +317 -0
- data/ext/rbs_extension/legacy_location.h +45 -0
- data/ext/rbs_extension/main.c +365 -14
- data/ext/rbs_extension/rbs_extension.h +6 -21
- data/ext/rbs_extension/rbs_string_bridging.c +9 -0
- data/ext/rbs_extension/rbs_string_bridging.h +24 -0
- data/include/rbs/ast.h +687 -0
- data/include/rbs/defines.h +86 -0
- data/include/rbs/lexer.h +199 -0
- data/include/rbs/location.h +59 -0
- data/include/rbs/parser.h +135 -0
- data/include/rbs/string.h +49 -0
- data/include/rbs/util/rbs_allocator.h +59 -0
- data/include/rbs/util/rbs_assert.h +20 -0
- data/include/rbs/util/rbs_buffer.h +83 -0
- data/include/rbs/util/rbs_constant_pool.h +6 -67
- data/include/rbs/util/rbs_encoding.h +282 -0
- data/include/rbs/util/rbs_unescape.h +23 -0
- data/include/rbs.h +1 -2
- data/lib/rbs/annotate/formatter.rb +3 -13
- data/lib/rbs/annotate/rdoc_annotator.rb +3 -1
- data/lib/rbs/annotate/rdoc_source.rb +1 -1
- data/lib/rbs/cli/validate.rb +2 -2
- data/lib/rbs/cli.rb +1 -1
- data/lib/rbs/collection/config/lockfile_generator.rb +1 -0
- data/lib/rbs/definition_builder/ancestor_builder.rb +5 -5
- data/lib/rbs/environment.rb +64 -59
- data/lib/rbs/environment_loader.rb +1 -1
- data/lib/rbs/errors.rb +1 -1
- data/lib/rbs/parser_aux.rb +5 -0
- data/lib/rbs/resolver/constant_resolver.rb +2 -2
- data/lib/rbs/resolver/type_name_resolver.rb +124 -38
- data/lib/rbs/test/type_check.rb +13 -0
- data/lib/rbs/types.rb +3 -1
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs.rb +1 -1
- data/lib/rdoc/discover.rb +1 -1
- data/lib/rdoc_plugin/parser.rb +3 -3
- data/sig/annotate/formatter.rbs +2 -2
- data/sig/annotate/rdoc_annotater.rbs +1 -1
- data/sig/environment.rbs +57 -6
- data/sig/manifest.yaml +0 -1
- data/sig/parser.rbs +20 -0
- data/sig/resolver/type_name_resolver.rbs +38 -7
- data/sig/types.rbs +4 -1
- data/src/ast.c +1256 -0
- data/src/lexer.c +2956 -0
- data/src/lexer.re +147 -0
- data/src/lexstate.c +205 -0
- data/src/location.c +71 -0
- data/src/parser.c +3495 -0
- data/src/string.c +90 -0
- data/src/util/rbs_allocator.c +152 -0
- data/src/util/rbs_assert.c +21 -0
- data/src/util/rbs_buffer.c +54 -0
- data/src/util/rbs_constant_pool.c +16 -86
- data/src/util/rbs_encoding.c +21308 -0
- data/src/util/rbs_unescape.c +131 -0
- data/stdlib/cgi/0/core.rbs +2 -396
- data/stdlib/cgi/0/manifest.yaml +1 -0
- data/stdlib/cgi-escape/0/escape.rbs +153 -0
- data/stdlib/coverage/0/coverage.rbs +3 -1
- data/stdlib/delegate/0/delegator.rbs +10 -7
- data/stdlib/erb/0/erb.rbs +737 -347
- data/stdlib/fileutils/0/fileutils.rbs +18 -13
- data/stdlib/forwardable/0/forwardable.rbs +3 -0
- data/stdlib/json/0/json.rbs +67 -48
- data/stdlib/net-http/0/net-http.rbs +3 -0
- data/stdlib/objspace/0/objspace.rbs +8 -3
- data/stdlib/open-uri/0/open-uri.rbs +40 -0
- data/stdlib/openssl/0/openssl.rbs +182 -149
- data/stdlib/optparse/0/optparse.rbs +3 -3
- data/stdlib/rdoc/0/code_object.rbs +2 -2
- data/stdlib/rdoc/0/comment.rbs +2 -0
- data/stdlib/rdoc/0/options.rbs +76 -0
- data/stdlib/rdoc/0/rdoc.rbs +7 -5
- data/stdlib/rdoc/0/store.rbs +1 -1
- data/stdlib/resolv/0/resolv.rbs +25 -68
- data/stdlib/ripper/0/ripper.rbs +5 -2
- data/stdlib/singleton/0/singleton.rbs +3 -0
- data/stdlib/socket/0/socket.rbs +13 -1
- data/stdlib/socket/0/tcp_socket.rbs +10 -2
- data/stdlib/stringio/0/stringio.rbs +412 -80
- data/stdlib/strscan/0/string_scanner.rbs +31 -31
- data/stdlib/tempfile/0/tempfile.rbs +1 -1
- data/stdlib/tsort/0/cyclic.rbs +3 -0
- data/stdlib/uri/0/common.rbs +11 -2
- data/stdlib/uri/0/file.rbs +1 -1
- data/stdlib/uri/0/generic.rbs +16 -15
- data/stdlib/uri/0/rfc2396_parser.rbs +6 -7
- data/stdlib/zlib/0/zstream.rbs +1 -0
- metadata +41 -18
- data/ext/rbs_extension/lexer.c +0 -2728
- data/ext/rbs_extension/lexer.h +0 -179
- data/ext/rbs_extension/lexer.re +0 -147
- data/ext/rbs_extension/lexstate.c +0 -175
- data/ext/rbs_extension/location.c +0 -325
- data/ext/rbs_extension/location.h +0 -85
- data/ext/rbs_extension/parser.c +0 -2982
- data/ext/rbs_extension/parser.h +0 -18
- data/ext/rbs_extension/parserstate.c +0 -411
- data/ext/rbs_extension/parserstate.h +0 -163
- data/ext/rbs_extension/unescape.c +0 -32
- data/include/rbs/ruby_objs.h +0 -72
- data/src/constants.c +0 -153
- data/src/ruby_objs.c +0 -799
data/stdlib/erb/0/erb.rbs
CHANGED
|
@@ -1,451 +1,804 @@
|
|
|
1
1
|
# <!-- rdoc-file=lib/erb.rb -->
|
|
2
|
-
#
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
#
|
|
8
|
-
# of generating document information details and/or flow control.
|
|
9
|
-
#
|
|
10
|
-
# A very simple example is this:
|
|
11
|
-
#
|
|
2
|
+
# Class **ERB** (the name stands for **Embedded Ruby**)
|
|
3
|
+
# is an easy-to-use, but also very powerful, [template
|
|
4
|
+
# processor](https://en.wikipedia.org/wiki/Template_processor).
|
|
5
|
+
# ## Usage
|
|
6
|
+
# Before you can use ERB, you must first require it
|
|
7
|
+
# (examples on this page assume that this has been done):
|
|
12
8
|
# require 'erb'
|
|
13
9
|
#
|
|
14
|
-
#
|
|
15
|
-
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
20
|
-
#
|
|
21
|
-
#
|
|
22
|
-
#
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
-
#
|
|
26
|
-
#
|
|
27
|
-
#
|
|
28
|
-
#
|
|
29
|
-
#
|
|
30
|
-
#
|
|
31
|
-
#
|
|
32
|
-
#
|
|
33
|
-
#
|
|
34
|
-
#
|
|
35
|
-
#
|
|
36
|
-
#
|
|
37
|
-
#
|
|
38
|
-
#
|
|
39
|
-
#
|
|
40
|
-
#
|
|
41
|
-
#
|
|
42
|
-
#
|
|
43
|
-
#
|
|
44
|
-
#
|
|
45
|
-
#
|
|
46
|
-
#
|
|
47
|
-
#
|
|
48
|
-
#
|
|
49
|
-
#
|
|
50
|
-
#
|
|
51
|
-
#
|
|
52
|
-
#
|
|
53
|
-
#
|
|
54
|
-
#
|
|
55
|
-
#
|
|
56
|
-
#
|
|
57
|
-
#
|
|
58
|
-
#
|
|
59
|
-
#
|
|
60
|
-
#
|
|
61
|
-
#
|
|
62
|
-
#
|
|
63
|
-
#
|
|
64
|
-
#
|
|
65
|
-
#
|
|
66
|
-
#
|
|
67
|
-
#
|
|
68
|
-
#
|
|
69
|
-
#
|
|
70
|
-
#
|
|
71
|
-
#
|
|
72
|
-
#
|
|
73
|
-
#
|
|
10
|
+
# ## In Brief
|
|
11
|
+
# Here's how ERB works:
|
|
12
|
+
# * You can create a *template*: a plain-text string that includes specially
|
|
13
|
+
# formatted *tags*..
|
|
14
|
+
# * You can create an ERB object to store the template.
|
|
15
|
+
# * You can call instance method ERB#result to get the *result*.
|
|
16
|
+
# ERB supports tags of three kinds:
|
|
17
|
+
# * [Expression tags](rdoc-ref:ERB@Expression+Tags):
|
|
18
|
+
# each begins with `'<%='`, ends with `'%>'`; contains a Ruby expression;
|
|
19
|
+
# in the result, the value of the expression replaces the entire tag:
|
|
20
|
+
# template = 'The magic word is <%= magic_word %>.'
|
|
21
|
+
# erb = ERB.new(template)
|
|
22
|
+
# magic_word = 'xyzzy'
|
|
23
|
+
# erb.result(binding) # => "The magic word is xyzzy."
|
|
24
|
+
#
|
|
25
|
+
# The above call to #result passes argument `binding`,
|
|
26
|
+
# which contains the binding of variable `magic_word` to its string value
|
|
27
|
+
# `'xyzzy'`.
|
|
28
|
+
# The below call to #result need not pass a binding,
|
|
29
|
+
# because its expression `Date::DAYNAMES` is globally defined.
|
|
30
|
+
# ERB.new('Today is <%= Date::DAYNAMES[Date.today.wday] %>.').result # => "Today is Monday."
|
|
31
|
+
#
|
|
32
|
+
# * [Execution tags](rdoc-ref:ERB@Execution+Tags):
|
|
33
|
+
# each begins with `'<%'`, ends with `'%>'`; contains Ruby code to be
|
|
34
|
+
# executed:
|
|
35
|
+
# template = '<% File.write("t.txt", "Some stuff.") %>'
|
|
36
|
+
# ERB.new(template).result
|
|
37
|
+
# File.read('t.txt') # => "Some stuff."
|
|
38
|
+
#
|
|
39
|
+
# * [Comment tags](rdoc-ref:ERB@Comment+Tags):
|
|
40
|
+
# each begins with `'<%#'`, ends with `'%>'`; contains comment text;
|
|
41
|
+
# in the result, the entire tag is omitted.
|
|
42
|
+
# template = 'Some stuff;<%# Note to self: figure out what the stuff is. %> more stuff.'
|
|
43
|
+
# ERB.new(template).result # => "Some stuff; more stuff."
|
|
44
|
+
#
|
|
45
|
+
# ## Some Simple Examples
|
|
46
|
+
# Here's a simple example of ERB in action:
|
|
47
|
+
# template = 'The time is <%= Time.now %>.'
|
|
48
|
+
# erb = ERB.new(template)
|
|
49
|
+
# erb.result
|
|
50
|
+
# # => "The time is 2025-09-09 10:49:26 -0500."
|
|
51
|
+
#
|
|
52
|
+
# Details:
|
|
53
|
+
# 1. A plain-text string is assigned to variable `template`.
|
|
54
|
+
# Its embedded [expression tag](rdoc-ref:ERB@Expression+Tags) `'<%=
|
|
55
|
+
# Time.now %>'` includes a Ruby expression, `Time.now`.
|
|
56
|
+
# 2. The string is put into a new ERB object, and stored in variable `erb`.
|
|
57
|
+
# 3. Method call `erb.result` generates a string that contains the run-time
|
|
58
|
+
# value of `Time.now`,
|
|
59
|
+
# as computed at the time of the call.
|
|
60
|
+
# The
|
|
61
|
+
# ERB object may be re-used:
|
|
62
|
+
# erb.result
|
|
63
|
+
# # => "The time is 2025-09-09 10:49:33 -0500."
|
|
64
|
+
#
|
|
65
|
+
# Another example:
|
|
66
|
+
# template = 'The magic word is <%= magic_word %>.'
|
|
67
|
+
# erb = ERB.new(template)
|
|
68
|
+
# magic_word = 'abracadabra'
|
|
69
|
+
# erb.result(binding)
|
|
70
|
+
# # => "The magic word is abracadabra."
|
|
71
|
+
#
|
|
72
|
+
# Details:
|
|
73
|
+
# 1. As before, a plain-text string is assigned to variable `template`.
|
|
74
|
+
# Its embedded [expression tag](rdoc-ref:ERB@Expression+Tags) `'<%=
|
|
75
|
+
# magic_word %>'` has a variable *name*, `magic_word`.
|
|
76
|
+
# 2. The string is put into a new ERB object, and stored in variable `erb`;
|
|
77
|
+
# note that `magic_word` need not be defined before the ERB object is
|
|
78
|
+
# created.
|
|
79
|
+
# 3. `magic_word = 'abracadabra'` assigns a value to variable `magic_word`.
|
|
80
|
+
# 4. Method call `erb.result(binding)` generates a string
|
|
81
|
+
# that contains the *value* of `magic_word`.
|
|
82
|
+
# As before, the ERB object may be re-used:
|
|
83
|
+
# magic_word = 'xyzzy'
|
|
84
|
+
# erb.result(binding)
|
|
85
|
+
# # => "The magic word is xyzzy."
|
|
86
|
+
#
|
|
87
|
+
# ## Bindings
|
|
88
|
+
# A call to method #result, which produces the formatted result string,
|
|
89
|
+
# requires a [Binding object](https://docs.ruby-lang.org/en/master/Binding.html)
|
|
90
|
+
# as its argument.
|
|
91
|
+
# The binding object provides the bindings for expressions in [expression
|
|
92
|
+
# tags](rdoc-ref:ERB@Expression+Tags).
|
|
93
|
+
# There are three ways to provide the required binding:
|
|
94
|
+
# * [Default binding](rdoc-ref:ERB@Default+Binding).
|
|
95
|
+
# * [Local binding](rdoc-ref:ERB@Local+Binding).
|
|
96
|
+
# * [Augmented binding](rdoc-ref:ERB@Augmented+Binding)
|
|
97
|
+
# ### Default Binding
|
|
98
|
+
# When you pass no `binding` argument to method #result,
|
|
99
|
+
# the method uses its default binding: the one returned by method #new_toplevel.
|
|
100
|
+
# This binding has the bindings defined by Ruby itself,
|
|
101
|
+
# which are those for Ruby's constants and variables.
|
|
102
|
+
# That binding is sufficient for an expression tag that refers only to Ruby's
|
|
103
|
+
# constants and variables;
|
|
104
|
+
# these expression tags refer only to Ruby's global constant `RUBY_COPYRIGHT`
|
|
105
|
+
# and global variable `$0`:
|
|
106
|
+
# template = <<TEMPLATE
|
|
107
|
+
# The Ruby copyright is <%= RUBY_COPYRIGHT.inspect %>.
|
|
108
|
+
# The current process is <%= $0 %>.
|
|
109
|
+
# TEMPLATE
|
|
110
|
+
# puts ERB.new(template).result
|
|
111
|
+
# The Ruby copyright is "ruby - Copyright (C) 1993-2025 Yukihiro Matsumoto".
|
|
112
|
+
# The current process is irb.
|
|
113
|
+
#
|
|
114
|
+
# (The current process is `irb` because that's where we're doing these
|
|
115
|
+
# examples!)
|
|
116
|
+
# ### Local Binding
|
|
117
|
+
# The default binding is *not* sufficient for an expression
|
|
118
|
+
# that refers to a a constant or variable that is not defined there:
|
|
119
|
+
# Foo = 1 # Defines local constant Foo.
|
|
120
|
+
# foo = 2 # Defines local variable foo.
|
|
121
|
+
# template = <<TEMPLATE
|
|
122
|
+
# The current value of constant Foo is <%= Foo %>.
|
|
123
|
+
# The current value of variable foo is <%= foo %>.
|
|
124
|
+
# The Ruby copyright is <%= RUBY_COPYRIGHT.inspect %>.
|
|
125
|
+
# The current process is <%= $0 %>.
|
|
126
|
+
# TEMPLATE
|
|
127
|
+
# erb = ERB.new(template)
|
|
128
|
+
#
|
|
129
|
+
# This call below raises `NameError` because although `Foo` and `foo` are
|
|
130
|
+
# defined locally,
|
|
131
|
+
# they are not defined in the default binding:
|
|
132
|
+
# erb.result # Raises NameError.
|
|
133
|
+
#
|
|
134
|
+
# To make the locally-defined constants and variables available,
|
|
135
|
+
# you can call #result with the local binding:
|
|
136
|
+
# puts erb.result(binding)
|
|
137
|
+
# The current value of constant Foo is 1.
|
|
138
|
+
# The current value of variable foo is 2.
|
|
139
|
+
# The Ruby copyright is "ruby - Copyright (C) 1993-2025 Yukihiro Matsumoto".
|
|
140
|
+
# The current process is irb.
|
|
141
|
+
#
|
|
142
|
+
# ### Augmented Binding
|
|
143
|
+
# Another way to make variable bindings (but not constant bindings) available
|
|
144
|
+
# is to use method #result_with_hash(hash);
|
|
145
|
+
# the passed hash has name/value pairs that are to be used to define and assign
|
|
146
|
+
# variables
|
|
147
|
+
# in a copy of the default binding:
|
|
148
|
+
# template = <<TEMPLATE
|
|
149
|
+
# The current value of variable bar is <%= bar %>.
|
|
150
|
+
# The current value of variable baz is <%= baz %>.
|
|
151
|
+
# The Ruby copyright is <%= RUBY_COPYRIGHT.inspect %>.
|
|
152
|
+
# The current process is <%= $0 %>.
|
|
153
|
+
# TEMPLATE
|
|
154
|
+
# erb = ERB.new(template)
|
|
155
|
+
#
|
|
156
|
+
# Both of these calls raise `NameError`, because `bar` and `baz`
|
|
157
|
+
# are not defined in either the default binding or the local binding.
|
|
158
|
+
# puts erb.result # Raises NameError.
|
|
159
|
+
# puts erb.result(binding) # Raises NameError.
|
|
160
|
+
#
|
|
161
|
+
# This call passes a hash that causes `bar` and `baz` to be defined
|
|
162
|
+
# in a new binding (derived from #new_toplevel):
|
|
163
|
+
# hash = {bar: 3, baz: 4}
|
|
164
|
+
# puts erb.result_with_hash(hash)
|
|
165
|
+
# The current value of variable bar is 3.
|
|
166
|
+
# The current value of variable baz is 4.
|
|
167
|
+
# The Ruby copyright is "ruby - Copyright (C) 1993-2025 Yukihiro Matsumoto".
|
|
168
|
+
# The current process is irb.
|
|
169
|
+
#
|
|
170
|
+
# ## Tags
|
|
171
|
+
# The examples above use expression tags.
|
|
172
|
+
# These are the tags available in ERB:
|
|
173
|
+
# * [Expression tag](rdoc-ref:ERB@Expression+Tags): the tag contains a Ruby
|
|
174
|
+
# expression;
|
|
175
|
+
# in the result, the entire tag is to be replaced with the run-time value
|
|
176
|
+
# of the expression.
|
|
177
|
+
# * [Execution tag](rdoc-ref:ERB@Execution+Tags): the tag contains Ruby code;
|
|
178
|
+
# in the result, the entire tag is to be replaced with the run-time value
|
|
179
|
+
# of the code.
|
|
180
|
+
# * [Comment tag](rdoc-ref:ERB@Comment+Tags): the tag contains comment code;
|
|
181
|
+
# in the result, the entire tag is to be omitted.
|
|
182
|
+
# ### Expression Tags
|
|
183
|
+
# You can embed a Ruby expression in a template using an *expression tag*.
|
|
184
|
+
# Its syntax is `<%= *expression* %>`,
|
|
185
|
+
# where *expression* is any valid Ruby expression.
|
|
186
|
+
# When you call method #result,
|
|
187
|
+
# the method evaluates the expression and replaces the entire expression tag
|
|
188
|
+
# with the expression's value:
|
|
189
|
+
# ERB.new('Today is <%= Date::DAYNAMES[Date.today.wday] %>.').result
|
|
190
|
+
# # => "Today is Monday."
|
|
191
|
+
# ERB.new('Tomorrow will be <%= Date::DAYNAMES[Date.today.wday + 1] %>.').result
|
|
192
|
+
# # => "Tomorrow will be Tuesday."
|
|
193
|
+
# ERB.new('Yesterday was <%= Date::DAYNAMES[Date.today.wday - 1] %>.').result
|
|
194
|
+
# # => "Yesterday was Sunday."
|
|
195
|
+
#
|
|
196
|
+
# Note that whitespace before and after the expression
|
|
197
|
+
# is allowed but not required,
|
|
198
|
+
# and that such whitespace is stripped from the result.
|
|
199
|
+
# ERB.new('My appointment is on <%=Date::DAYNAMES[Date.today.wday + 2]%>.').result
|
|
200
|
+
# # => "My appointment is on Wednesday."
|
|
201
|
+
# ERB.new('My appointment is on <%= Date::DAYNAMES[Date.today.wday + 2] %>.').result
|
|
202
|
+
# # => "My appointment is on Wednesday."
|
|
203
|
+
#
|
|
204
|
+
# ### Execution Tags
|
|
205
|
+
# You can embed Ruby executable code in template using an *execution tag*.
|
|
206
|
+
# Its syntax is `<% *code* %>`,
|
|
207
|
+
# where *code* is any valid Ruby code.
|
|
208
|
+
# When you call method #result,
|
|
209
|
+
# the method executes the code and removes the entire execution tag
|
|
210
|
+
# (generating no text in the result):
|
|
211
|
+
# ERB.new('foo <% Dir.chdir("C:/") %> bar').result # => "foo bar"
|
|
212
|
+
#
|
|
213
|
+
# Whitespace before and after the embedded code is optional:
|
|
214
|
+
# ERB.new('foo <%Dir.chdir("C:/")%> bar').result # => "foo bar"
|
|
215
|
+
#
|
|
216
|
+
# You can interleave text with execution tags to form a control structure
|
|
217
|
+
# such as a conditional, a loop, or a `case` statements.
|
|
218
|
+
# Conditional:
|
|
219
|
+
# template = <<TEMPLATE
|
|
220
|
+
# <% if verbosity %>
|
|
221
|
+
# An error has occurred.
|
|
222
|
+
# <% else %>
|
|
223
|
+
# Oops!
|
|
224
|
+
# <% end %>
|
|
225
|
+
# TEMPLATE
|
|
226
|
+
# erb = ERB.new(template)
|
|
227
|
+
# verbosity = true
|
|
228
|
+
# erb.result(binding)
|
|
229
|
+
# # => "\nAn error has occurred.\n\n"
|
|
230
|
+
# verbosity = false
|
|
231
|
+
# erb.result(binding)
|
|
232
|
+
# # => "\nOops!\n\n"
|
|
233
|
+
#
|
|
234
|
+
# Note that the interleaved text may itself contain expression tags:
|
|
235
|
+
# Loop:
|
|
236
|
+
# template = <<TEMPLATE
|
|
237
|
+
# <% Date::ABBR_DAYNAMES.each do |dayname| %>
|
|
238
|
+
# <%= dayname %>
|
|
239
|
+
# <% end %>
|
|
240
|
+
# TEMPLATE
|
|
241
|
+
# ERB.new(template).result
|
|
242
|
+
# # => "\nSun\n\nMon\n\nTue\n\nWed\n\nThu\n\nFri\n\nSat\n\n"
|
|
243
|
+
#
|
|
244
|
+
# Other, non-control, lines of Ruby code may be interleaved with the text,
|
|
245
|
+
# and the Ruby code may itself contain regular Ruby comments:
|
|
246
|
+
# template = <<TEMPLATE
|
|
247
|
+
# <% 3.times do %>
|
|
248
|
+
# <%= Time.now %>
|
|
249
|
+
# <% sleep(1) # Let's make the times different. %>
|
|
250
|
+
# <% end %>
|
|
251
|
+
# TEMPLATE
|
|
252
|
+
# ERB.new(template).result
|
|
253
|
+
# # => "\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"
|
|
254
|
+
#
|
|
255
|
+
# The execution tag may also contain multiple lines of code:
|
|
256
|
+
# template = <<TEMPLATE
|
|
257
|
+
# <%
|
|
258
|
+
# (0..2).each do |i|
|
|
259
|
+
# (0..2).each do |j|
|
|
260
|
+
# %>
|
|
261
|
+
# * <%=i%>,<%=j%>
|
|
262
|
+
# <%
|
|
263
|
+
# end
|
|
264
|
+
# end
|
|
265
|
+
# %>
|
|
266
|
+
# TEMPLATE
|
|
267
|
+
# ERB.new(template).result
|
|
268
|
+
# # => "\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"
|
|
269
|
+
#
|
|
270
|
+
# #### Shorthand Format for Execution Tags
|
|
271
|
+
# You can use keyword argument `trim_mode: '%'` to enable a shorthand format for
|
|
272
|
+
# execution tags;
|
|
273
|
+
# this example uses the shorthand format `% *code`* instead of `<% *code* %>`:
|
|
274
|
+
# template = <<TEMPLATE
|
|
275
|
+
# % priorities.each do |priority|
|
|
276
|
+
# * <%= priority %>
|
|
277
|
+
# % end
|
|
278
|
+
# TEMPLATE
|
|
279
|
+
# erb = ERB.new(template, trim_mode: '%')
|
|
280
|
+
# priorities = [ 'Run Ruby Quiz',
|
|
281
|
+
# 'Document Modules',
|
|
282
|
+
# 'Answer Questions on Ruby Talk' ]
|
|
283
|
+
# puts erb.result(binding)
|
|
284
|
+
# * Run Ruby Quiz
|
|
285
|
+
# * Document Modules
|
|
286
|
+
# * Answer Questions on Ruby Talk
|
|
287
|
+
#
|
|
288
|
+
# Note that in the shorthand format, the character `'%'` must be the first
|
|
289
|
+
# character in the code line
|
|
290
|
+
# (no leading whitespace).
|
|
291
|
+
# #### Suppressing Unwanted Blank Lines
|
|
292
|
+
# With keyword argument `trim_mode` not given,
|
|
293
|
+
# all blank lines go into the result:
|
|
294
|
+
# template = <<TEMPLATE
|
|
295
|
+
# <% if true %>
|
|
296
|
+
# <%= RUBY_VERSION %>
|
|
297
|
+
# <% end %>
|
|
298
|
+
# TEMPLATE
|
|
299
|
+
# ERB.new(template).result.lines.each {|line| puts line.inspect }
|
|
300
|
+
# "\n"
|
|
301
|
+
# "3.4.5\n"
|
|
302
|
+
# "\n"
|
|
303
|
+
#
|
|
304
|
+
# You can give `trim_mode: '-'`, you can suppress each blank line
|
|
305
|
+
# whose source line ends with `-%>` (instead of `%>`):
|
|
306
|
+
# template = <<TEMPLATE
|
|
307
|
+
# <% if true -%>
|
|
308
|
+
# <%= RUBY_VERSION %>
|
|
309
|
+
# <% end -%>
|
|
310
|
+
# TEMPLATE
|
|
311
|
+
# ERB.new(template, trim_mode: '-').result.lines.each {|line| puts line.inspect }
|
|
312
|
+
# "3.4.5\n"
|
|
313
|
+
#
|
|
314
|
+
# It is an error to use the trailing `'-%>'` notation without `trim_mode: '-'`:
|
|
315
|
+
# ERB.new(template).result.lines.each {|line| puts line.inspect } # Raises SyntaxError.
|
|
316
|
+
#
|
|
317
|
+
# #### Suppressing Unwanted Newlines
|
|
318
|
+
# Consider this template:
|
|
319
|
+
# template = <<TEMPLATE
|
|
320
|
+
# <% RUBY_VERSION %>
|
|
321
|
+
# <%= RUBY_VERSION %>
|
|
322
|
+
# foo <% RUBY_VERSION %>
|
|
323
|
+
# foo <%= RUBY_VERSION %>
|
|
324
|
+
# TEMPLATE
|
|
325
|
+
#
|
|
326
|
+
# With keyword argument `trim_mode` not given, all newlines go into the result:
|
|
327
|
+
# ERB.new(template).result.lines.each {|line| puts line.inspect }
|
|
328
|
+
# "\n"
|
|
329
|
+
# "3.4.5\n"
|
|
330
|
+
# "foo \n"
|
|
331
|
+
# "foo 3.4.5\n"
|
|
332
|
+
#
|
|
333
|
+
# You can give `trim_mode: '>'` to suppress the trailing newline
|
|
334
|
+
# for each line that ends with `'%>'` (regardless of its beginning):
|
|
335
|
+
# ERB.new(template, trim_mode: '>').result.lines.each {|line| puts line.inspect }
|
|
336
|
+
# "3.4.5foo foo 3.4.5"
|
|
337
|
+
#
|
|
338
|
+
# You can give `trim_mode: '<>'` to suppress the trailing newline
|
|
339
|
+
# for each line that both begins with `'<%'` and ends with `'%>'`:
|
|
340
|
+
# ERB.new(template, trim_mode: '<>').result.lines.each {|line| puts line.inspect }
|
|
341
|
+
# "3.4.5foo \n"
|
|
342
|
+
# "foo 3.4.5\n"
|
|
343
|
+
#
|
|
344
|
+
# #### Combining Trim Modes
|
|
345
|
+
# You can combine certain trim modes:
|
|
346
|
+
# * `'%-'`: Enable shorthand and omit each blank line ending with `'-%>'`.
|
|
347
|
+
# * `'%>'`: Enable shorthand and omit newline for each line ending with
|
|
348
|
+
# `'%>'`.
|
|
349
|
+
# * `'%<>'`: Enable shorthand and omit newline for each line starting with
|
|
350
|
+
# `'<%'` and ending with `'%>'`.
|
|
351
|
+
# ### Comment Tags
|
|
352
|
+
# You can embed a comment in a template using a *comment tag*;
|
|
353
|
+
# its syntax is `<%# *text* %>`,
|
|
354
|
+
# where *text* is the text of the comment.
|
|
355
|
+
# When you call method #result,
|
|
356
|
+
# it removes the entire comment tag
|
|
357
|
+
# (generating no text in the result).
|
|
358
|
+
# Example:
|
|
359
|
+
# template = 'Some stuff;<%# Note to self: figure out what the stuff is. %> more stuff.'
|
|
360
|
+
# ERB.new(template).result # => "Some stuff; more stuff."
|
|
361
|
+
#
|
|
362
|
+
# A comment tag may appear anywhere in the template.
|
|
363
|
+
# Note that the beginning of the tag must be `'<%#'`, not `'<% #'`.
|
|
364
|
+
# In this example, the tag begins with `'<% #'`, and so is an execution tag, not
|
|
365
|
+
# a comment tag;
|
|
366
|
+
# the cited code consists entirely of a Ruby-style comment (which is of course
|
|
367
|
+
# ignored):
|
|
368
|
+
# ERB.new('Some stuff;<% # Note to self: figure out what the stuff is. %> more stuff.').result
|
|
369
|
+
# # => "Some stuff;"
|
|
370
|
+
#
|
|
371
|
+
# ## Encodings
|
|
372
|
+
# An ERB object has an
|
|
373
|
+
# [encoding](https://docs.ruby-lang.org/en/master/Encoding.html),
|
|
374
|
+
# which is by default the encoding of the template string;
|
|
375
|
+
# the result string will also have that encoding.
|
|
376
|
+
# template = <<TEMPLATE
|
|
377
|
+
# <%# Comment. %>
|
|
378
|
+
# TEMPLATE
|
|
379
|
+
# erb = ERB.new(template)
|
|
380
|
+
# template.encoding # => #<Encoding:UTF-8>
|
|
381
|
+
# erb.encoding # => #<Encoding:UTF-8>
|
|
382
|
+
# erb.result.encoding # => #<Encoding:UTF-8>
|
|
383
|
+
#
|
|
384
|
+
# You can specify a different encoding by adding a [magic
|
|
385
|
+
# comment](https://docs.ruby-lang.org/en/master/syntax/comments_rdoc.html#label-
|
|
386
|
+
# Magic+Comments)
|
|
387
|
+
# at the top of the given template:
|
|
388
|
+
# template = <<TEMPLATE
|
|
389
|
+
# <%#-*- coding: Big5 -*-%>
|
|
390
|
+
# <%# Comment. %>
|
|
391
|
+
# TEMPLATE
|
|
392
|
+
# erb = ERB.new(template)
|
|
393
|
+
# template.encoding # => #<Encoding:UTF-8>
|
|
394
|
+
# erb.encoding # => #<Encoding:Big5>
|
|
395
|
+
# erb.result.encoding # => #<Encoding:Big5>
|
|
396
|
+
#
|
|
397
|
+
# ## Error Reporting
|
|
398
|
+
# Consider this template (containing an error):
|
|
399
|
+
# template = '<%= nosuch %>'
|
|
400
|
+
# erb = ERB.new(template)
|
|
401
|
+
#
|
|
402
|
+
# When ERB reports an error,
|
|
403
|
+
# it includes a file name (if available) and a line number;
|
|
404
|
+
# the file name comes from method #filename, the line number from method
|
|
405
|
+
# #lineno.
|
|
406
|
+
# Initially, those values are `nil` and `0`, respectively;
|
|
407
|
+
# these initial values are reported as `'(erb)'` and `1`, respectively:
|
|
408
|
+
# erb.filename # => nil
|
|
409
|
+
# erb.lineno # => 0
|
|
410
|
+
# erb.result
|
|
411
|
+
# (erb):1:in '<main>': undefined local variable or method 'nosuch' for main (NameError)
|
|
412
|
+
#
|
|
413
|
+
# You can use methods #filename= and #lineno= to assign values
|
|
414
|
+
# that are more meaningful in your context:
|
|
415
|
+
# erb.filename = 't.txt'
|
|
416
|
+
# erb.lineno = 555
|
|
417
|
+
# erb.result
|
|
418
|
+
# t.txt:556:in '<main>': undefined local variable or method 'nosuch' for main (NameError)
|
|
419
|
+
#
|
|
420
|
+
# You can use method #location= to set both values:
|
|
421
|
+
# erb.location = ['u.txt', 999]
|
|
422
|
+
# erb.result
|
|
423
|
+
# u.txt:1000:in '<main>': undefined local variable or method 'nosuch' for main (NameError)
|
|
424
|
+
#
|
|
425
|
+
# ## Plain Text with Embedded Ruby
|
|
426
|
+
# Here's a plain-text template;
|
|
427
|
+
# it uses the literal notation `'%q{ ... }'` to define the template
|
|
428
|
+
# (see [%q
|
|
429
|
+
# literals](https://docs.ruby-lang.org/en/master/syntax/literals_rdoc.html#label
|
|
430
|
+
# -25q-3A+Non-Interpolable+String+Literals));
|
|
431
|
+
# this avoids problems with backslashes.
|
|
74
432
|
# template = %q{
|
|
75
|
-
#
|
|
76
|
-
#
|
|
77
|
-
#
|
|
78
|
-
#
|
|
79
|
-
# <%= to[/\w+/] %>:
|
|
433
|
+
# From: James Edward Gray II <james@grayproductions.net>
|
|
434
|
+
# To: <%= to %>
|
|
435
|
+
# Subject: Addressing Needs
|
|
80
436
|
#
|
|
81
|
-
#
|
|
82
|
-
# addressed.
|
|
437
|
+
# <%= to[/\w+/] %>:
|
|
83
438
|
#
|
|
84
|
-
#
|
|
85
|
-
#
|
|
439
|
+
# Just wanted to send a quick note assuring that your needs are being
|
|
440
|
+
# addressed.
|
|
86
441
|
#
|
|
87
|
-
#
|
|
88
|
-
#
|
|
89
|
-
# * <%= priority %>
|
|
90
|
-
# % end
|
|
442
|
+
# I want you to know that my team will keep working on the issues,
|
|
443
|
+
# especially:
|
|
91
444
|
#
|
|
92
|
-
#
|
|
445
|
+
# <%# ignore numerous minor requests -- focus on priorities %>
|
|
446
|
+
# % priorities.each do |priority|
|
|
447
|
+
# * <%= priority %>
|
|
448
|
+
# % end
|
|
93
449
|
#
|
|
94
|
-
#
|
|
95
|
-
# }.gsub(/^ /, '')
|
|
450
|
+
# Thanks for your patience.
|
|
96
451
|
#
|
|
97
|
-
#
|
|
452
|
+
# James Edward Gray II
|
|
453
|
+
# }
|
|
98
454
|
#
|
|
99
|
-
#
|
|
100
|
-
# to =
|
|
101
|
-
#
|
|
102
|
-
#
|
|
103
|
-
#
|
|
455
|
+
# The template will need these:
|
|
456
|
+
# to = 'Community Spokesman <spokesman@ruby_community.org>'
|
|
457
|
+
# priorities = [ 'Run Ruby Quiz',
|
|
458
|
+
# 'Document Modules',
|
|
459
|
+
# 'Answer Questions on Ruby Talk' ]
|
|
104
460
|
#
|
|
105
|
-
#
|
|
106
|
-
#
|
|
107
|
-
#
|
|
461
|
+
# Finally, create the ERB object and get the result
|
|
462
|
+
# erb = ERB.new(template, trim_mode: '%<>')
|
|
463
|
+
# puts erb.result(binding)
|
|
108
464
|
#
|
|
109
|
-
#
|
|
465
|
+
# From: James Edward Gray II <james@grayproductions.net>
|
|
466
|
+
# To: Community Spokesman <spokesman@ruby_community.org>
|
|
467
|
+
# Subject: Addressing Needs
|
|
110
468
|
#
|
|
111
|
-
#
|
|
112
|
-
# To: Community Spokesman <spokesman@ruby_community.org>
|
|
113
|
-
# Subject: Addressing Needs
|
|
469
|
+
# Community:
|
|
114
470
|
#
|
|
115
|
-
#
|
|
471
|
+
# Just wanted to send a quick note assuring that your needs are being
|
|
472
|
+
# addressed.
|
|
116
473
|
#
|
|
117
|
-
#
|
|
474
|
+
# I want you to know that my team will keep working on the issues,
|
|
475
|
+
# especially:
|
|
118
476
|
#
|
|
119
|
-
#
|
|
477
|
+
# * Run Ruby Quiz
|
|
478
|
+
# * Document Modules
|
|
479
|
+
# * Answer Questions on Ruby Talk
|
|
120
480
|
#
|
|
121
|
-
#
|
|
122
|
-
# * Document Modules
|
|
123
|
-
# * Answer Questions on Ruby Talk
|
|
481
|
+
# Thanks for your patience.
|
|
124
482
|
#
|
|
125
|
-
#
|
|
483
|
+
# James Edward Gray II
|
|
126
484
|
#
|
|
127
|
-
#
|
|
128
|
-
#
|
|
129
|
-
#
|
|
130
|
-
#
|
|
131
|
-
# ERB is often used in `.rhtml` files (HTML with embedded Ruby). Notice the
|
|
132
|
-
# need in this example to provide a special binding when the template is run, so
|
|
133
|
-
# that the instance variables in the Product object can be resolved.
|
|
134
|
-
#
|
|
135
|
-
# require "erb"
|
|
136
|
-
#
|
|
137
|
-
# # Build template data class.
|
|
485
|
+
# ## HTML with Embedded Ruby
|
|
486
|
+
# This example shows an HTML template.
|
|
487
|
+
# First, here's a custom class, `Product`:
|
|
138
488
|
# class Product
|
|
139
|
-
#
|
|
140
|
-
#
|
|
141
|
-
#
|
|
142
|
-
#
|
|
143
|
-
#
|
|
144
|
-
#
|
|
145
|
-
#
|
|
146
|
-
#
|
|
147
|
-
#
|
|
148
|
-
#
|
|
149
|
-
#
|
|
150
|
-
#
|
|
151
|
-
#
|
|
152
|
-
#
|
|
153
|
-
#
|
|
154
|
-
#
|
|
155
|
-
#
|
|
156
|
-
#
|
|
157
|
-
#
|
|
158
|
-
#
|
|
159
|
-
#
|
|
160
|
-
#
|
|
161
|
-
#
|
|
162
|
-
#
|
|
163
|
-
#
|
|
164
|
-
#
|
|
165
|
-
#
|
|
166
|
-
#
|
|
167
|
-
#
|
|
168
|
-
#
|
|
169
|
-
#
|
|
170
|
-
#
|
|
171
|
-
#
|
|
172
|
-
#
|
|
173
|
-
#
|
|
174
|
-
#
|
|
175
|
-
#
|
|
176
|
-
#
|
|
177
|
-
#
|
|
178
|
-
#
|
|
179
|
-
#
|
|
180
|
-
#
|
|
181
|
-
#
|
|
182
|
-
#
|
|
183
|
-
#
|
|
184
|
-
#
|
|
185
|
-
#
|
|
186
|
-
#
|
|
187
|
-
#
|
|
188
|
-
#
|
|
189
|
-
#
|
|
190
|
-
#
|
|
191
|
-
#
|
|
192
|
-
#
|
|
193
|
-
#
|
|
194
|
-
#
|
|
195
|
-
#
|
|
196
|
-
#
|
|
197
|
-
#
|
|
198
|
-
#
|
|
199
|
-
#
|
|
200
|
-
#
|
|
201
|
-
#
|
|
202
|
-
#
|
|
203
|
-
#
|
|
204
|
-
#
|
|
205
|
-
#
|
|
206
|
-
#
|
|
207
|
-
#
|
|
208
|
-
#
|
|
209
|
-
#
|
|
210
|
-
#
|
|
211
|
-
#
|
|
212
|
-
#
|
|
213
|
-
#
|
|
214
|
-
#
|
|
215
|
-
#
|
|
216
|
-
#
|
|
217
|
-
#
|
|
218
|
-
#
|
|
219
|
-
#
|
|
220
|
-
#
|
|
221
|
-
# Call for a price, today!
|
|
222
|
-
# </p>
|
|
223
|
-
#
|
|
224
|
-
# </body>
|
|
225
|
-
# </html>
|
|
226
|
-
#
|
|
227
|
-
# ## Notes
|
|
228
|
-
#
|
|
229
|
-
# There are a variety of templating solutions available in various Ruby
|
|
230
|
-
# projects. For example, RDoc, distributed with Ruby, uses its own template
|
|
231
|
-
# engine, which can be reused elsewhere.
|
|
232
|
-
#
|
|
233
|
-
# Other popular engines could be found in the corresponding
|
|
234
|
-
# [Category](https://www.ruby-toolbox.com/categories/template_engines) of The
|
|
235
|
-
# Ruby Toolbox.
|
|
489
|
+
# def initialize(code, name, desc, cost)
|
|
490
|
+
# @code = code
|
|
491
|
+
# @name = name
|
|
492
|
+
# @desc = desc
|
|
493
|
+
# @cost = cost
|
|
494
|
+
# @features = []
|
|
495
|
+
# end
|
|
496
|
+
#
|
|
497
|
+
# def add_feature(feature)
|
|
498
|
+
# @features << feature
|
|
499
|
+
# end
|
|
500
|
+
#
|
|
501
|
+
# # Support templating of member data.
|
|
502
|
+
# def get_binding
|
|
503
|
+
# binding
|
|
504
|
+
# end
|
|
505
|
+
#
|
|
506
|
+
# end
|
|
507
|
+
#
|
|
508
|
+
# The template below will need these values:
|
|
509
|
+
# toy = Product.new('TZ-1002',
|
|
510
|
+
# 'Rubysapien',
|
|
511
|
+
# "Geek's Best Friend! Responds to Ruby commands...",
|
|
512
|
+
# 999.95
|
|
513
|
+
# )
|
|
514
|
+
# toy.add_feature('Listens for verbal commands in the Ruby language!')
|
|
515
|
+
# toy.add_feature('Ignores Perl, Java, and all C variants.')
|
|
516
|
+
# toy.add_feature('Karate-Chop Action!!!')
|
|
517
|
+
# toy.add_feature('Matz signature on left leg.')
|
|
518
|
+
# toy.add_feature('Gem studded eyes... Rubies, of course!')
|
|
519
|
+
#
|
|
520
|
+
# Here's the HTML:
|
|
521
|
+
# template = <<TEMPLATE
|
|
522
|
+
# <html>
|
|
523
|
+
# <head><title>Ruby Toys -- <%= @name %></title></head>
|
|
524
|
+
# <body>
|
|
525
|
+
# <h1><%= @name %> (<%= @code %>)</h1>
|
|
526
|
+
# <p><%= @desc %></p>
|
|
527
|
+
# <ul>
|
|
528
|
+
# <% @features.each do |f| %>
|
|
529
|
+
# <li><b><%= f %></b></li>
|
|
530
|
+
# <% end %>
|
|
531
|
+
# </ul>
|
|
532
|
+
# <p>
|
|
533
|
+
# <% if @cost < 10 %>
|
|
534
|
+
# <b>Only <%= @cost %>!!!</b>
|
|
535
|
+
# <% else %>
|
|
536
|
+
# Call for a price, today!
|
|
537
|
+
# <% end %>
|
|
538
|
+
# </p>
|
|
539
|
+
# </body>
|
|
540
|
+
# </html>
|
|
541
|
+
# TEMPLATE
|
|
542
|
+
#
|
|
543
|
+
# Finally, create the ERB object and get the result (omitting some blank lines):
|
|
544
|
+
# erb = ERB.new(template)
|
|
545
|
+
# puts erb.result(toy.get_binding)
|
|
546
|
+
# <html>
|
|
547
|
+
# <head><title>Ruby Toys -- Rubysapien</title></head>
|
|
548
|
+
# <body>
|
|
549
|
+
# <h1>Rubysapien (TZ-1002)</h1>
|
|
550
|
+
# <p>Geek's Best Friend! Responds to Ruby commands...</p>
|
|
551
|
+
# <ul>
|
|
552
|
+
# <li><b>Listens for verbal commands in the Ruby language!</b></li>
|
|
553
|
+
# <li><b>Ignores Perl, Java, and all C variants.</b></li>
|
|
554
|
+
# <li><b>Karate-Chop Action!!!</b></li>
|
|
555
|
+
# <li><b>Matz signature on left leg.</b></li>
|
|
556
|
+
# <li><b>Gem studded eyes... Rubies, of course!</b></li>
|
|
557
|
+
# </ul>
|
|
558
|
+
# <p>
|
|
559
|
+
# Call for a price, today!
|
|
560
|
+
# </p>
|
|
561
|
+
# </body>
|
|
562
|
+
# </html>
|
|
563
|
+
#
|
|
564
|
+
# ## Other Template Processors
|
|
565
|
+
# Various Ruby projects have their own template processors.
|
|
566
|
+
# The Ruby Processing System [RDoc](https://ruby.github.io/rdoc), for example,
|
|
567
|
+
# has one that can be used elsewhere.
|
|
568
|
+
# Other popular template processors may found in the [Template
|
|
569
|
+
# Engines](https://www.ruby-toolbox.com/categories/template_engines) page
|
|
570
|
+
# of the Ruby Toolbox.
|
|
236
571
|
#
|
|
237
572
|
class ERB
|
|
238
573
|
# <!--
|
|
239
574
|
# rdoc-file=lib/erb.rb
|
|
240
|
-
# - version
|
|
575
|
+
# - self.version -> string
|
|
241
576
|
# -->
|
|
242
|
-
# Returns
|
|
577
|
+
# Returns the string ERB version.
|
|
243
578
|
#
|
|
244
579
|
def self.version: () -> String
|
|
245
580
|
|
|
246
581
|
# <!--
|
|
247
582
|
# rdoc-file=lib/erb.rb
|
|
248
|
-
# - new(
|
|
583
|
+
# - ERB.new(template, trim_mode: nil, eoutvar: '_erbout')
|
|
249
584
|
# -->
|
|
250
|
-
#
|
|
251
|
-
#
|
|
252
|
-
#
|
|
253
|
-
#
|
|
254
|
-
#
|
|
255
|
-
#
|
|
256
|
-
#
|
|
257
|
-
#
|
|
258
|
-
#
|
|
259
|
-
#
|
|
260
|
-
#
|
|
261
|
-
#
|
|
262
|
-
#
|
|
263
|
-
# *
|
|
264
|
-
#
|
|
265
|
-
#
|
|
266
|
-
#
|
|
267
|
-
#
|
|
268
|
-
#
|
|
269
|
-
#
|
|
270
|
-
#
|
|
271
|
-
#
|
|
272
|
-
#
|
|
273
|
-
#
|
|
274
|
-
#
|
|
275
|
-
#
|
|
276
|
-
# :cost => 9.95 }
|
|
277
|
-
#
|
|
278
|
-
# attr_reader :product, :price
|
|
279
|
-
#
|
|
280
|
-
# def initialize( product = "", price = "" )
|
|
281
|
-
# @product = product
|
|
282
|
-
# @price = price
|
|
283
|
-
# end
|
|
284
|
-
#
|
|
285
|
-
# def build
|
|
286
|
-
# b = binding
|
|
287
|
-
# # create and run templates, filling member data variables
|
|
288
|
-
# ERB.new(<<~'END_PRODUCT', trim_mode: "", eoutvar: "@product").result b
|
|
289
|
-
# <%= PRODUCT[:name] %>
|
|
290
|
-
# <%= PRODUCT[:desc] %>
|
|
291
|
-
# END_PRODUCT
|
|
292
|
-
# ERB.new(<<~'END_PRICE', trim_mode: "", eoutvar: "@price").result b
|
|
293
|
-
# <%= PRODUCT[:name] %> -- <%= PRODUCT[:cost] %>
|
|
294
|
-
# <%= PRODUCT[:desc] %>
|
|
295
|
-
# END_PRICE
|
|
296
|
-
# end
|
|
297
|
-
# end
|
|
298
|
-
#
|
|
299
|
-
# # setup template data
|
|
300
|
-
# listings = Listings.new
|
|
301
|
-
# listings.build
|
|
302
|
-
#
|
|
303
|
-
# puts listings.product + "\n" + listings.price
|
|
304
|
-
#
|
|
305
|
-
# *Generates*
|
|
306
|
-
#
|
|
307
|
-
# Chicken Fried Steak
|
|
308
|
-
# A well messages pattie, breaded and fried.
|
|
309
|
-
#
|
|
310
|
-
# Chicken Fried Steak -- 9.95
|
|
311
|
-
# A well messages pattie, breaded and fried.
|
|
585
|
+
# Returns a new ERB object containing the given string `template`.
|
|
586
|
+
# For details about `template`, its embedded tags, and generated results, see
|
|
587
|
+
# ERB.
|
|
588
|
+
# **Keyword Argument `trim_mode`**
|
|
589
|
+
# You can use keyword argument `trim_mode: '%'`
|
|
590
|
+
# to enable the [shorthand
|
|
591
|
+
# format](rdoc-ref:ERB@Shorthand+Format+for+Execution+Tags) for execution tags.
|
|
592
|
+
# This value allows [blank line
|
|
593
|
+
# control](rdoc-ref:ERB@Suppressing+Unwanted+Blank+Lines):
|
|
594
|
+
# * `'-'`: Omit each blank line ending with `'%>'`.
|
|
595
|
+
# Other values allow [newline
|
|
596
|
+
# control](rdoc-ref:ERB@Suppressing+Unwanted+Newlines):
|
|
597
|
+
# * `'>'`: Omit newline for each line ending with `'%>'`.
|
|
598
|
+
# * `'<>'`: Omit newline for each line starting with `'<%'` and ending with
|
|
599
|
+
# `'%>'`.
|
|
600
|
+
# You can also [combine trim modes](rdoc-ref:ERB@Combining+Trim+Modes).
|
|
601
|
+
# **Keyword Argument `eoutvar`**
|
|
602
|
+
# The string value of keyword argument `eoutvar` specifies the name of the
|
|
603
|
+
# variable
|
|
604
|
+
# that method #result uses to construct its result string;
|
|
605
|
+
# see #src.
|
|
606
|
+
# This is useful when you need to run multiple ERB templates through the same
|
|
607
|
+
# binding
|
|
608
|
+
# and/or when you want to control where output ends up.
|
|
609
|
+
# It's good practice to choose a variable name that begins with an underscore:
|
|
610
|
+
# `'_'`.
|
|
312
611
|
#
|
|
313
612
|
def initialize: (String, ?eoutvar: String, ?trim_mode: Integer | String | NilClass) -> untyped
|
|
314
613
|
|
|
315
614
|
# <!-- rdoc-file=lib/erb.rb -->
|
|
316
|
-
#
|
|
615
|
+
# Returns the Ruby code that, when executed, generates the result;
|
|
616
|
+
# the code is executed by method #result,
|
|
617
|
+
# and by its wrapper methods #result_with_hash and #run:
|
|
618
|
+
# template = 'The time is <%= Time.now %>.'
|
|
619
|
+
# erb = ERB.new(template)
|
|
620
|
+
# erb.src
|
|
621
|
+
# # => "#coding:UTF-8\n_erbout = +''; _erbout.<< \"The time is \".freeze; _erbout.<<(( Time.now ).to_s); _erbout.<< \".\".freeze; _erbout"
|
|
622
|
+
# erb.result
|
|
623
|
+
# # => "The time is 2025-09-18 15:58:08 -0500."
|
|
624
|
+
#
|
|
625
|
+
# In a more readable format:
|
|
626
|
+
# # puts erb.src.split('; ')
|
|
627
|
+
# # #coding:UTF-8
|
|
628
|
+
# # _erbout = +''
|
|
629
|
+
# # _erbout.<< "The time is ".freeze
|
|
630
|
+
# # _erbout.<<(( Time.now ).to_s)
|
|
631
|
+
# # _erbout.<< ".".freeze
|
|
632
|
+
# # _erbout
|
|
633
|
+
#
|
|
634
|
+
# Variable `_erbout` is used to store the intermediate results in the code;
|
|
635
|
+
# the name `_erbout` is the default in ERB.new,
|
|
636
|
+
# and can be changed via keyword argument `eoutvar`:
|
|
637
|
+
# erb = ERB.new(template, eoutvar: '_foo')
|
|
638
|
+
# puts template.src.split('; ')
|
|
639
|
+
# #coding:UTF-8
|
|
640
|
+
# _foo = +''
|
|
641
|
+
# _foo.<< "The time is ".freeze
|
|
642
|
+
# _foo.<<(( Time.now ).to_s)
|
|
643
|
+
# _foo.<< ".".freeze
|
|
644
|
+
# _foo
|
|
317
645
|
#
|
|
318
646
|
def src: () -> String
|
|
319
647
|
|
|
320
648
|
# <!-- rdoc-file=lib/erb.rb -->
|
|
321
|
-
#
|
|
649
|
+
# Returns the encoding of `self`;
|
|
650
|
+
# see [Encodings](rdoc-ref:ERB@Encodings):
|
|
322
651
|
#
|
|
323
652
|
def encoding: () -> Encoding
|
|
324
653
|
|
|
325
654
|
# <!-- rdoc-file=lib/erb.rb -->
|
|
326
|
-
#
|
|
327
|
-
#
|
|
655
|
+
# Sets or returns the file name to be used in reporting errors;
|
|
656
|
+
# see [Error Reporting](rdoc-ref:ERB@Error+Reporting).
|
|
328
657
|
#
|
|
329
658
|
def filename: () -> (String | NilClass)
|
|
330
659
|
|
|
331
660
|
# <!-- rdoc-file=lib/erb.rb -->
|
|
332
|
-
#
|
|
333
|
-
#
|
|
661
|
+
# Sets or returns the file name to be used in reporting errors;
|
|
662
|
+
# see [Error Reporting](rdoc-ref:ERB@Error+Reporting).
|
|
334
663
|
#
|
|
335
664
|
def filename=: (String | NilClass) -> untyped
|
|
336
665
|
|
|
337
666
|
# <!-- rdoc-file=lib/erb.rb -->
|
|
338
|
-
#
|
|
667
|
+
# Sets or returns the line number to be used in reporting errors;
|
|
668
|
+
# see [Error Reporting](rdoc-ref:ERB@Error+Reporting).
|
|
339
669
|
#
|
|
340
670
|
def lineno: () -> Integer
|
|
341
671
|
|
|
342
672
|
# <!-- rdoc-file=lib/erb.rb -->
|
|
343
|
-
#
|
|
673
|
+
# Sets or returns the line number to be used in reporting errors;
|
|
674
|
+
# see [Error Reporting](rdoc-ref:ERB@Error+Reporting).
|
|
344
675
|
#
|
|
345
676
|
def lineno=: (Integer) -> untyped
|
|
346
677
|
|
|
347
678
|
# <!--
|
|
348
679
|
# rdoc-file=lib/erb.rb
|
|
349
|
-
# - location=
|
|
680
|
+
# - location = [filename, lineno] => [filename, lineno]
|
|
681
|
+
# - location = filename -> filename
|
|
350
682
|
# -->
|
|
351
|
-
# Sets
|
|
352
|
-
#
|
|
353
|
-
#
|
|
354
|
-
# erb = ERB.new('<%= some_x %>')
|
|
355
|
-
# erb.render
|
|
356
|
-
# # undefined local variable or method `some_x'
|
|
357
|
-
# # from (erb):1
|
|
358
|
-
#
|
|
359
|
-
# erb.location = ['file.erb', 3]
|
|
360
|
-
# # All subsequent error reporting would use new location
|
|
361
|
-
# erb.render
|
|
362
|
-
# # undefined local variable or method `some_x'
|
|
363
|
-
# # from file.erb:4
|
|
683
|
+
# Sets the values of #filename and, if given, #lineno;
|
|
684
|
+
# see [Error Reporting](rdoc-ref:ERB@Error+Reporting).
|
|
364
685
|
#
|
|
365
686
|
def location=: (Array[String | Integer]) -> untyped
|
|
366
687
|
|
|
367
688
|
# <!--
|
|
368
689
|
# rdoc-file=lib/erb.rb
|
|
369
|
-
# - run(
|
|
690
|
+
# - run(binding = new_toplevel) -> nil
|
|
370
691
|
# -->
|
|
371
|
-
#
|
|
692
|
+
# Like #result, but prints the result string (instead of returning it);
|
|
693
|
+
# returns `nil`.
|
|
372
694
|
#
|
|
373
695
|
def run: (?Binding) -> untyped
|
|
374
696
|
|
|
375
697
|
# <!--
|
|
376
698
|
# rdoc-file=lib/erb.rb
|
|
377
|
-
# - result(
|
|
699
|
+
# - result(binding = new_toplevel) -> new_string
|
|
378
700
|
# -->
|
|
379
|
-
#
|
|
380
|
-
#
|
|
381
|
-
#
|
|
382
|
-
#
|
|
383
|
-
#
|
|
384
|
-
#
|
|
701
|
+
# Returns the string result formed by processing ERB tags found in the stored
|
|
702
|
+
# template in `self`.
|
|
703
|
+
# With no argument given, uses the default binding;
|
|
704
|
+
# see [Default Binding](rdoc-ref:ERB@Default+Binding).
|
|
705
|
+
# With argument `binding` given, uses the local binding;
|
|
706
|
+
# see [Local Binding](rdoc-ref:ERB@Local+Binding).
|
|
707
|
+
# See also #result_with_hash.
|
|
385
708
|
#
|
|
386
709
|
def result: (?Binding) -> String
|
|
387
710
|
|
|
388
711
|
# <!--
|
|
389
712
|
# rdoc-file=lib/erb.rb
|
|
390
|
-
# - result_with_hash(hash)
|
|
713
|
+
# - result_with_hash(hash) -> new_string
|
|
391
714
|
# -->
|
|
392
|
-
#
|
|
393
|
-
#
|
|
715
|
+
# Returns the string result formed by processing ERB tags found in the stored
|
|
716
|
+
# string in `self`;
|
|
717
|
+
# see [Augmented Binding](rdoc-ref:ERB@Augmented+Binding).
|
|
718
|
+
# See also #result.
|
|
394
719
|
#
|
|
395
720
|
def result_with_hash: (Hash[untyped, untyped]) -> String
|
|
396
721
|
|
|
397
722
|
# <!--
|
|
398
723
|
# rdoc-file=lib/erb.rb
|
|
399
|
-
# - def_method(
|
|
724
|
+
# - def_method(module, method_signature, filename = '(ERB)') -> method_name
|
|
400
725
|
# -->
|
|
401
|
-
#
|
|
402
|
-
#
|
|
403
|
-
#
|
|
404
|
-
#
|
|
405
|
-
#
|
|
406
|
-
#
|
|
407
|
-
#
|
|
726
|
+
# Creates and returns a new instance method in the given module `module`;
|
|
727
|
+
# returns the method name as a symbol.
|
|
728
|
+
# The method is created from the given `method_signature`,
|
|
729
|
+
# which consists of the method name and its argument names (if any).
|
|
730
|
+
# The `filename` sets the value of #filename;
|
|
731
|
+
# see [Error Reporting](rdoc-ref:ERB@Error+Reporting).
|
|
732
|
+
# template = '<%= arg1 %> <%= arg2 %>'
|
|
733
|
+
# erb = ERB.new(template)
|
|
734
|
+
# MyModule = Module.new
|
|
735
|
+
# erb.def_method(MyModule, 'render(arg1, arg2)') # => :render
|
|
736
|
+
# class MyClass; include MyModule; end
|
|
737
|
+
# MyClass.new.render('foo', 123) # => "foo 123"
|
|
408
738
|
#
|
|
409
739
|
def def_method: (Module, String, ?String) -> untyped
|
|
410
740
|
|
|
411
741
|
# <!--
|
|
412
742
|
# rdoc-file=lib/erb.rb
|
|
413
|
-
# - def_module(
|
|
743
|
+
# - def_module(method_name = 'erb') -> new_module
|
|
414
744
|
# -->
|
|
415
|
-
#
|
|
416
|
-
#
|
|
417
|
-
#
|
|
418
|
-
#
|
|
419
|
-
#
|
|
420
|
-
#
|
|
421
|
-
#
|
|
422
|
-
#
|
|
423
|
-
#
|
|
424
|
-
# include MyModule
|
|
425
|
-
# end
|
|
745
|
+
# Returns a new nameless module that has instance method `method_name`.
|
|
746
|
+
# template = '<%= arg1 %> <%= arg2 %>'
|
|
747
|
+
# erb = ERB.new(template)
|
|
748
|
+
# MyModule = template.def_module('render(arg1, arg2)')
|
|
749
|
+
# class MyClass
|
|
750
|
+
# include MyModule
|
|
751
|
+
# end
|
|
752
|
+
# MyClass.new.render('foo', 123)
|
|
753
|
+
# # => "foo 123"
|
|
426
754
|
#
|
|
427
755
|
def def_module: (?String) -> Module
|
|
428
756
|
|
|
429
757
|
# <!--
|
|
430
758
|
# rdoc-file=lib/erb.rb
|
|
431
|
-
# - def_class(
|
|
759
|
+
# - def_class(super_class = Object, method_name = 'result') -> new_class
|
|
432
760
|
# -->
|
|
433
|
-
#
|
|
434
|
-
#
|
|
435
|
-
#
|
|
436
|
-
#
|
|
437
|
-
#
|
|
438
|
-
#
|
|
439
|
-
#
|
|
440
|
-
#
|
|
441
|
-
#
|
|
442
|
-
#
|
|
443
|
-
#
|
|
444
|
-
#
|
|
445
|
-
#
|
|
761
|
+
# Returns a new nameless class whose superclass is `super_class`,
|
|
762
|
+
# and which has instance method `method_name`.
|
|
763
|
+
# Create a template from HTML that has embedded expression tags that use `@arg1`
|
|
764
|
+
# and `@arg2`:
|
|
765
|
+
# html = <<TEMPLATE
|
|
766
|
+
# <html>
|
|
767
|
+
# <body>
|
|
768
|
+
# <p><%= @arg1 %></p>
|
|
769
|
+
# <p><%= @arg2 %></p>
|
|
770
|
+
# </body>
|
|
771
|
+
# </html>
|
|
772
|
+
# TEMPLATE
|
|
773
|
+
# template = ERB.new(html)
|
|
774
|
+
#
|
|
775
|
+
# Create a base class that has `@arg1` and `@arg2`:
|
|
776
|
+
# class MyBaseClass
|
|
777
|
+
# def initialize(arg1, arg2)
|
|
778
|
+
# @arg1 = arg1
|
|
779
|
+
# @arg2 = arg2
|
|
780
|
+
# end
|
|
781
|
+
# end
|
|
782
|
+
#
|
|
783
|
+
# Use method #def_class to create a subclass that has method `:render`:
|
|
784
|
+
# MySubClass = template.def_class(MyBaseClass, :render)
|
|
785
|
+
#
|
|
786
|
+
# Generate the result:
|
|
787
|
+
# puts MySubClass.new('foo', 123).render
|
|
788
|
+
# <html>
|
|
789
|
+
# <body>
|
|
790
|
+
# <p>foo</p>
|
|
791
|
+
# <p>123</p>
|
|
792
|
+
# </body>
|
|
793
|
+
# </html>
|
|
446
794
|
#
|
|
447
795
|
def def_class: (?Class, ?String) -> Class
|
|
448
796
|
|
|
797
|
+
# <!-- rdoc-file=lib/erb/util.rb -->
|
|
798
|
+
# ERB::Util
|
|
799
|
+
#
|
|
800
|
+
# A utility module for conversion routines, often handy in HTML generation.
|
|
801
|
+
#
|
|
449
802
|
module Util
|
|
450
803
|
# <!--
|
|
451
804
|
# rdoc-file=lib/erb.rb
|
|
@@ -510,6 +863,37 @@ class ERB
|
|
|
510
863
|
alias self.u self.url_encode
|
|
511
864
|
end
|
|
512
865
|
|
|
866
|
+
# <!-- rdoc-file=lib/erb/def_method.rb -->
|
|
867
|
+
# ERB::DefMethod
|
|
868
|
+
#
|
|
869
|
+
# Utility module to define eRuby script as instance method.
|
|
870
|
+
#
|
|
871
|
+
# ### Example
|
|
872
|
+
#
|
|
873
|
+
# example.rhtml:
|
|
874
|
+
# <% for item in @items %>
|
|
875
|
+
# <b><%= item %></b>
|
|
876
|
+
# <% end %>
|
|
877
|
+
#
|
|
878
|
+
# example.rb:
|
|
879
|
+
# require 'erb'
|
|
880
|
+
# class MyClass
|
|
881
|
+
# extend ERB::DefMethod
|
|
882
|
+
# def_erb_method('render()', 'example.rhtml')
|
|
883
|
+
# def initialize(items)
|
|
884
|
+
# @items = items
|
|
885
|
+
# end
|
|
886
|
+
# end
|
|
887
|
+
# print MyClass.new([10,20,30]).render()
|
|
888
|
+
#
|
|
889
|
+
# result:
|
|
890
|
+
#
|
|
891
|
+
# <b>10</b>
|
|
892
|
+
#
|
|
893
|
+
# <b>20</b>
|
|
894
|
+
#
|
|
895
|
+
# <b>30</b>
|
|
896
|
+
#
|
|
513
897
|
module DefMethod
|
|
514
898
|
# <!--
|
|
515
899
|
# rdoc-file=lib/erb/def_method.rb
|
|
@@ -521,6 +905,12 @@ class ERB
|
|
|
521
905
|
def self.def_erb_method: (String methodname, (String | ERB) erb_or_fname) -> untyped
|
|
522
906
|
end
|
|
523
907
|
|
|
908
|
+
# <!-- rdoc-file=lib/erb/util.rb -->
|
|
909
|
+
# ERB::Escape
|
|
910
|
+
#
|
|
911
|
+
# A subset of ERB::Util. Unlike ERB::Util#html_escape, we expect/hope Rails will
|
|
912
|
+
# not monkey-patch ERB::Escape#html_escape.
|
|
913
|
+
#
|
|
524
914
|
module Escape
|
|
525
915
|
# <!--
|
|
526
916
|
# rdoc-file=lib/erb/util.rb
|