rbs 3.10.0 → 4.0.0.dev.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/.github/workflows/comments.yml +3 -3
- data/.github/workflows/ruby.yml +24 -35
- data/.github/workflows/typecheck.yml +3 -1
- data/.github/workflows/windows.yml +2 -2
- data/.gitignore +0 -4
- data/CHANGELOG.md +0 -88
- data/README.md +1 -38
- data/Rakefile +20 -142
- data/Steepfile +1 -0
- data/config.yml +43 -1
- data/core/array.rbs +46 -100
- data/core/complex.rbs +21 -32
- data/core/dir.rbs +2 -2
- data/core/encoding.rbs +9 -6
- data/core/enumerable.rbs +3 -90
- data/core/enumerator.rbs +1 -18
- data/core/errno.rbs +0 -8
- data/core/errors.rbs +1 -28
- data/core/exception.rbs +2 -2
- data/core/fiber.rbs +4 -5
- data/core/file.rbs +12 -27
- data/core/file_test.rbs +1 -1
- data/core/float.rbs +22 -209
- data/core/gc.rbs +281 -417
- data/core/hash.rbs +727 -1024
- data/core/integer.rbs +38 -78
- data/core/io/buffer.rbs +7 -18
- data/core/io/wait.rbs +33 -11
- data/core/io.rbs +12 -14
- data/core/kernel.rbs +51 -57
- data/core/marshal.rbs +1 -1
- data/core/match_data.rbs +1 -1
- data/core/math.rbs +3 -42
- data/core/method.rbs +6 -14
- data/core/module.rbs +17 -88
- data/core/nil_class.rbs +3 -3
- data/core/numeric.rbs +16 -16
- data/core/object.rbs +3 -3
- data/core/object_space.rbs +15 -21
- data/core/proc.rbs +8 -15
- data/core/process.rbs +2 -2
- data/core/ractor.rbs +437 -278
- data/core/range.rbs +8 -7
- data/core/rational.rbs +24 -37
- data/core/rbs/unnamed/argf.rbs +2 -2
- data/core/rbs/unnamed/env_class.rbs +1 -1
- data/core/rbs/unnamed/random.rbs +2 -4
- data/core/regexp.rbs +20 -25
- data/core/ruby_vm.rbs +4 -6
- data/core/rubygems/errors.rbs +70 -3
- data/core/rubygems/rubygems.rbs +79 -11
- data/core/rubygems/version.rbs +3 -2
- data/core/set.rbs +359 -488
- data/core/string.rbs +1228 -3153
- data/core/struct.rbs +1 -1
- data/core/symbol.rbs +4 -4
- data/core/thread.rbs +29 -92
- data/core/time.rbs +9 -35
- data/core/trace_point.rbs +4 -7
- data/core/unbound_method.rbs +6 -14
- data/docs/collection.md +2 -2
- data/docs/gem.md +1 -0
- data/docs/sigs.md +3 -3
- data/ext/rbs_extension/ast_translation.c +1077 -944
- data/ext/rbs_extension/ast_translation.h +0 -7
- data/ext/rbs_extension/class_constants.c +83 -71
- data/ext/rbs_extension/class_constants.h +7 -4
- data/ext/rbs_extension/extconf.rb +2 -24
- data/ext/rbs_extension/legacy_location.c +172 -173
- data/ext/rbs_extension/legacy_location.h +3 -8
- data/ext/rbs_extension/main.c +289 -239
- data/ext/rbs_extension/rbs_extension.h +0 -3
- data/ext/rbs_extension/rbs_string_bridging.h +0 -4
- data/include/rbs/ast.h +98 -37
- data/include/rbs/defines.h +12 -38
- data/include/rbs/lexer.h +114 -126
- data/include/rbs/location.h +14 -14
- data/include/rbs/parser.h +37 -21
- data/include/rbs/string.h +5 -3
- data/include/rbs/util/rbs_allocator.h +19 -40
- data/include/rbs/util/rbs_assert.h +1 -12
- data/include/rbs/util/rbs_constant_pool.h +3 -3
- data/include/rbs/util/rbs_encoding.h +1 -3
- data/include/rbs/util/rbs_unescape.h +1 -2
- data/lib/rbs/ast/ruby/annotations.rb +119 -0
- data/lib/rbs/ast/ruby/comment_block.rb +221 -0
- data/lib/rbs/ast/ruby/declarations.rb +86 -0
- data/lib/rbs/ast/ruby/helpers/constant_helper.rb +24 -0
- data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
- data/lib/rbs/ast/ruby/members.rb +213 -0
- data/lib/rbs/buffer.rb +104 -24
- data/lib/rbs/cli/validate.rb +40 -35
- data/lib/rbs/cli.rb +5 -6
- data/lib/rbs/collection/config/lockfile_generator.rb +0 -1
- data/lib/rbs/collection.rb +0 -1
- data/lib/rbs/definition.rb +6 -1
- data/lib/rbs/definition_builder/ancestor_builder.rb +65 -62
- data/lib/rbs/definition_builder/method_builder.rb +45 -30
- data/lib/rbs/definition_builder.rb +44 -9
- data/lib/rbs/environment/class_entry.rb +69 -0
- data/lib/rbs/environment/module_entry.rb +66 -0
- data/lib/rbs/environment.rb +244 -218
- data/lib/rbs/environment_loader.rb +3 -3
- data/lib/rbs/errors.rb +5 -4
- data/lib/rbs/inline_parser/comment_association.rb +117 -0
- data/lib/rbs/inline_parser.rb +206 -0
- data/lib/rbs/location_aux.rb +35 -3
- data/lib/rbs/parser_aux.rb +11 -6
- data/lib/rbs/prototype/runtime.rb +2 -2
- data/lib/rbs/resolver/constant_resolver.rb +2 -2
- data/lib/rbs/resolver/type_name_resolver.rb +38 -124
- data/lib/rbs/source.rb +99 -0
- data/lib/rbs/subtractor.rb +4 -3
- data/lib/rbs/test/type_check.rb +0 -14
- data/lib/rbs/types.rb +1 -3
- data/lib/rbs/version.rb +1 -1
- data/lib/rbs.rb +13 -1
- data/lib/rdoc/discover.rb +1 -1
- data/lib/rdoc_plugin/parser.rb +1 -1
- data/rbs.gemspec +1 -0
- data/sig/ancestor_builder.rbs +1 -1
- data/sig/ast/ruby/annotations.rbs +110 -0
- data/sig/ast/ruby/comment_block.rbs +119 -0
- data/sig/ast/ruby/declarations.rbs +60 -0
- data/sig/ast/ruby/helpers/constant_helper.rbs +11 -0
- data/sig/ast/ruby/helpers/location_helper.rbs +15 -0
- data/sig/ast/ruby/members.rbs +72 -0
- data/sig/buffer.rbs +63 -5
- data/sig/definition.rbs +1 -0
- data/sig/definition_builder.rbs +1 -1
- data/sig/environment/class_entry.rbs +50 -0
- data/sig/environment/module_entry.rbs +50 -0
- data/sig/environment.rbs +28 -133
- data/sig/errors.rbs +13 -6
- data/sig/inline_parser/comment_association.rbs +71 -0
- data/sig/inline_parser.rbs +87 -0
- data/sig/location.rbs +32 -7
- data/sig/manifest.yaml +1 -0
- data/sig/method_builder.rbs +7 -4
- data/sig/parser.rbs +16 -20
- data/sig/resolver/type_name_resolver.rbs +7 -38
- data/sig/source.rbs +48 -0
- data/sig/types.rbs +1 -4
- data/src/ast.c +290 -201
- data/src/lexer.c +2813 -2902
- data/src/lexer.re +4 -0
- data/src/lexstate.c +155 -169
- data/src/location.c +40 -40
- data/src/parser.c +2665 -2433
- data/src/string.c +48 -0
- data/src/util/rbs_allocator.c +77 -80
- data/src/util/rbs_assert.c +10 -10
- data/src/util/rbs_buffer.c +2 -2
- data/src/util/rbs_constant_pool.c +15 -13
- data/src/util/rbs_encoding.c +4062 -20097
- data/src/util/rbs_unescape.c +48 -85
- data/stdlib/bigdecimal/0/big_decimal.rbs +82 -100
- data/stdlib/bigdecimal-math/0/big_math.rbs +8 -169
- data/stdlib/cgi/0/core.rbs +396 -2
- data/stdlib/cgi/0/manifest.yaml +0 -1
- data/stdlib/coverage/0/coverage.rbs +1 -3
- data/stdlib/date/0/date.rbs +59 -67
- data/stdlib/date/0/date_time.rbs +1 -1
- data/stdlib/delegate/0/delegator.rbs +7 -10
- data/stdlib/erb/0/erb.rbs +347 -737
- data/stdlib/fileutils/0/fileutils.rbs +13 -18
- data/stdlib/forwardable/0/forwardable.rbs +0 -3
- data/stdlib/json/0/json.rbs +48 -68
- data/stdlib/net-http/0/net-http.rbs +0 -3
- data/stdlib/objspace/0/objspace.rbs +4 -9
- data/stdlib/open-uri/0/open-uri.rbs +0 -40
- data/stdlib/openssl/0/openssl.rbs +228 -331
- data/stdlib/optparse/0/optparse.rbs +3 -3
- data/{core → stdlib/pathname/0}/pathname.rbs +355 -255
- data/stdlib/psych/0/psych.rbs +3 -3
- data/stdlib/rdoc/0/rdoc.rbs +1 -1
- data/stdlib/resolv/0/resolv.rbs +68 -25
- data/stdlib/ripper/0/ripper.rbs +2 -5
- data/stdlib/singleton/0/singleton.rbs +0 -3
- data/stdlib/socket/0/socket.rbs +1 -13
- data/stdlib/socket/0/tcp_socket.rbs +2 -10
- data/stdlib/stringio/0/stringio.rbs +85 -1176
- data/stdlib/strscan/0/string_scanner.rbs +31 -31
- data/stdlib/tempfile/0/tempfile.rbs +3 -3
- data/stdlib/time/0/time.rbs +1 -1
- data/stdlib/timeout/0/timeout.rbs +7 -63
- data/stdlib/tsort/0/cyclic.rbs +0 -3
- data/stdlib/uri/0/common.rbs +2 -11
- data/stdlib/uri/0/file.rbs +1 -1
- data/stdlib/uri/0/generic.rbs +16 -17
- data/stdlib/uri/0/rfc2396_parser.rbs +7 -6
- data/stdlib/zlib/0/zstream.rbs +0 -1
- metadata +40 -12
- data/.clang-format +0 -74
- data/.clangd +0 -2
- data/.github/workflows/c-check.yml +0 -54
- data/core/ruby.rbs +0 -53
- data/docs/aliases.md +0 -79
- data/docs/encoding.md +0 -56
- data/ext/rbs_extension/compat.h +0 -10
- data/stdlib/cgi-escape/0/escape.rbs +0 -153
data/stdlib/erb/0/erb.rbs
CHANGED
|
@@ -1,804 +1,451 @@
|
|
|
1
1
|
# <!-- rdoc-file=lib/erb.rb -->
|
|
2
|
-
#
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
#
|
|
6
|
-
#
|
|
7
|
-
#
|
|
2
|
+
# # ERB -- Ruby Templating
|
|
3
|
+
#
|
|
4
|
+
# ## Introduction
|
|
5
|
+
#
|
|
6
|
+
# ERB provides an easy to use but powerful templating system for Ruby. Using
|
|
7
|
+
# ERB, actual Ruby code can be added to any plain text document for the purposes
|
|
8
|
+
# of generating document information details and/or flow control.
|
|
9
|
+
#
|
|
10
|
+
# A very simple example is this:
|
|
11
|
+
#
|
|
8
12
|
# require 'erb'
|
|
9
13
|
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
#
|
|
13
|
-
#
|
|
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
|
-
# erb
|
|
50
|
-
#
|
|
51
|
-
#
|
|
52
|
-
#
|
|
53
|
-
#
|
|
54
|
-
#
|
|
55
|
-
#
|
|
56
|
-
#
|
|
57
|
-
#
|
|
58
|
-
#
|
|
59
|
-
#
|
|
60
|
-
#
|
|
61
|
-
#
|
|
62
|
-
#
|
|
63
|
-
#
|
|
64
|
-
#
|
|
65
|
-
#
|
|
66
|
-
#
|
|
67
|
-
# erb
|
|
68
|
-
#
|
|
69
|
-
#
|
|
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.
|
|
14
|
+
# x = 42
|
|
15
|
+
# template = ERB.new <<-EOF
|
|
16
|
+
# The value of x is: <%= x %>
|
|
17
|
+
# EOF
|
|
18
|
+
# puts template.result(binding)
|
|
19
|
+
#
|
|
20
|
+
# *Prints:* The value of x is: 42
|
|
21
|
+
#
|
|
22
|
+
# More complex examples are given below.
|
|
23
|
+
#
|
|
24
|
+
# ## Recognized Tags
|
|
25
|
+
#
|
|
26
|
+
# ERB recognizes certain tags in the provided template and converts them based
|
|
27
|
+
# on the rules below:
|
|
28
|
+
#
|
|
29
|
+
# <% Ruby code -- inline with output %>
|
|
30
|
+
# <%= Ruby expression -- replace with result %>
|
|
31
|
+
# <%# comment -- ignored -- useful in testing %> (`<% #` doesn't work. Don't use Ruby comments.)
|
|
32
|
+
# % a line of Ruby code -- treated as <% line %> (optional -- see ERB.new)
|
|
33
|
+
# %% replaced with % if first thing on a line and % processing is used
|
|
34
|
+
# <%% or %%> -- replace with <% or %> respectively
|
|
35
|
+
#
|
|
36
|
+
# All other text is passed through ERB filtering unchanged.
|
|
37
|
+
#
|
|
38
|
+
# ## Options
|
|
39
|
+
#
|
|
40
|
+
# There are several settings you can change when you use ERB:
|
|
41
|
+
# * the nature of the tags that are recognized;
|
|
42
|
+
# * the binding used to resolve local variables in the template.
|
|
43
|
+
#
|
|
44
|
+
# See the ERB.new and ERB#result methods for more detail.
|
|
45
|
+
#
|
|
46
|
+
# ## Character encodings
|
|
47
|
+
#
|
|
48
|
+
# ERB (or Ruby code generated by ERB) returns a string in the same character
|
|
49
|
+
# encoding as the input string. When the input string has a magic comment,
|
|
50
|
+
# however, it returns a string in the encoding specified by the magic comment.
|
|
51
|
+
#
|
|
52
|
+
# # -*- coding: utf-8 -*-
|
|
53
|
+
# require 'erb'
|
|
54
|
+
#
|
|
55
|
+
# template = ERB.new <<EOF
|
|
56
|
+
# <%#-*- coding: Big5 -*-%>
|
|
57
|
+
# \_\_ENCODING\_\_ is <%= \_\_ENCODING\_\_ %>.
|
|
58
|
+
# EOF
|
|
59
|
+
# puts template.result
|
|
60
|
+
#
|
|
61
|
+
# *Prints:* _*ENCODING*_ is Big5.
|
|
62
|
+
#
|
|
63
|
+
# ## Examples
|
|
64
|
+
#
|
|
65
|
+
# ### Plain Text
|
|
66
|
+
#
|
|
67
|
+
# ERB is useful for any generic templating situation. Note that in this
|
|
68
|
+
# example, we use the convenient "% at start of line" tag, and we quote the
|
|
69
|
+
# template literally with `%q{...}` to avoid trouble with the backslash.
|
|
70
|
+
#
|
|
71
|
+
# require "erb"
|
|
72
|
+
#
|
|
73
|
+
# # Create template.
|
|
432
74
|
# template = %q{
|
|
433
|
-
#
|
|
434
|
-
#
|
|
435
|
-
#
|
|
75
|
+
# From: James Edward Gray II <james@grayproductions.net>
|
|
76
|
+
# To: <%= to %>
|
|
77
|
+
# Subject: Addressing Needs
|
|
78
|
+
#
|
|
79
|
+
# <%= to[/\w+/] %>:
|
|
436
80
|
#
|
|
437
|
-
#
|
|
81
|
+
# Just wanted to send a quick note assuring that your needs are being
|
|
82
|
+
# addressed.
|
|
438
83
|
#
|
|
439
|
-
#
|
|
440
|
-
#
|
|
84
|
+
# I want you to know that my team will keep working on the issues,
|
|
85
|
+
# especially:
|
|
441
86
|
#
|
|
442
|
-
#
|
|
443
|
-
#
|
|
87
|
+
# <%# ignore numerous minor requests -- focus on priorities %>
|
|
88
|
+
# % priorities.each do |priority|
|
|
89
|
+
# * <%= priority %>
|
|
90
|
+
# % end
|
|
444
91
|
#
|
|
445
|
-
#
|
|
446
|
-
# % priorities.each do |priority|
|
|
447
|
-
# * <%= priority %>
|
|
448
|
-
# % end
|
|
92
|
+
# Thanks for your patience.
|
|
449
93
|
#
|
|
450
|
-
#
|
|
94
|
+
# James Edward Gray II
|
|
95
|
+
# }.gsub(/^ /, '')
|
|
451
96
|
#
|
|
452
|
-
#
|
|
453
|
-
# }
|
|
97
|
+
# message = ERB.new(template, trim_mode: "%<>")
|
|
454
98
|
#
|
|
455
|
-
#
|
|
456
|
-
# to =
|
|
457
|
-
#
|
|
458
|
-
#
|
|
459
|
-
#
|
|
99
|
+
# # Set up template data.
|
|
100
|
+
# to = "Community Spokesman <spokesman@ruby_community.org>"
|
|
101
|
+
# priorities = [ "Run Ruby Quiz",
|
|
102
|
+
# "Document Modules",
|
|
103
|
+
# "Answer Questions on Ruby Talk" ]
|
|
460
104
|
#
|
|
461
|
-
#
|
|
462
|
-
#
|
|
463
|
-
#
|
|
105
|
+
# # Produce result.
|
|
106
|
+
# email = message.result
|
|
107
|
+
# puts email
|
|
464
108
|
#
|
|
465
|
-
#
|
|
466
|
-
# To: Community Spokesman <spokesman@ruby_community.org>
|
|
467
|
-
# Subject: Addressing Needs
|
|
109
|
+
# *Generates:*
|
|
468
110
|
#
|
|
469
|
-
#
|
|
111
|
+
# From: James Edward Gray II <james@grayproductions.net>
|
|
112
|
+
# To: Community Spokesman <spokesman@ruby_community.org>
|
|
113
|
+
# Subject: Addressing Needs
|
|
470
114
|
#
|
|
471
|
-
#
|
|
472
|
-
# addressed.
|
|
115
|
+
# Community:
|
|
473
116
|
#
|
|
474
|
-
#
|
|
475
|
-
# especially:
|
|
117
|
+
# Just wanted to send a quick note assuring that your needs are being addressed.
|
|
476
118
|
#
|
|
477
|
-
#
|
|
478
|
-
# * Document Modules
|
|
479
|
-
# * Answer Questions on Ruby Talk
|
|
119
|
+
# I want you to know that my team will keep working on the issues, especially:
|
|
480
120
|
#
|
|
481
|
-
#
|
|
121
|
+
# * Run Ruby Quiz
|
|
122
|
+
# * Document Modules
|
|
123
|
+
# * Answer Questions on Ruby Talk
|
|
482
124
|
#
|
|
483
|
-
#
|
|
125
|
+
# Thanks for your patience.
|
|
484
126
|
#
|
|
485
|
-
#
|
|
486
|
-
#
|
|
487
|
-
#
|
|
127
|
+
# James Edward Gray II
|
|
128
|
+
#
|
|
129
|
+
# ### Ruby in HTML
|
|
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.
|
|
488
138
|
# class Product
|
|
489
|
-
#
|
|
490
|
-
#
|
|
491
|
-
#
|
|
492
|
-
#
|
|
493
|
-
#
|
|
494
|
-
#
|
|
495
|
-
#
|
|
496
|
-
#
|
|
497
|
-
#
|
|
498
|
-
#
|
|
499
|
-
#
|
|
500
|
-
#
|
|
501
|
-
#
|
|
502
|
-
#
|
|
503
|
-
#
|
|
504
|
-
#
|
|
505
|
-
#
|
|
506
|
-
#
|
|
507
|
-
#
|
|
508
|
-
#
|
|
509
|
-
#
|
|
510
|
-
#
|
|
511
|
-
#
|
|
512
|
-
#
|
|
513
|
-
#
|
|
514
|
-
#
|
|
515
|
-
#
|
|
516
|
-
#
|
|
517
|
-
#
|
|
518
|
-
#
|
|
519
|
-
#
|
|
520
|
-
#
|
|
521
|
-
#
|
|
522
|
-
#
|
|
523
|
-
#
|
|
524
|
-
#
|
|
525
|
-
#
|
|
526
|
-
#
|
|
527
|
-
#
|
|
528
|
-
#
|
|
529
|
-
#
|
|
530
|
-
#
|
|
531
|
-
#
|
|
532
|
-
#
|
|
533
|
-
#
|
|
534
|
-
#
|
|
535
|
-
#
|
|
536
|
-
#
|
|
537
|
-
#
|
|
538
|
-
#
|
|
539
|
-
#
|
|
540
|
-
#
|
|
541
|
-
#
|
|
542
|
-
#
|
|
543
|
-
#
|
|
544
|
-
#
|
|
545
|
-
#
|
|
546
|
-
#
|
|
547
|
-
#
|
|
548
|
-
#
|
|
549
|
-
#
|
|
550
|
-
#
|
|
551
|
-
#
|
|
552
|
-
#
|
|
553
|
-
#
|
|
554
|
-
#
|
|
555
|
-
#
|
|
556
|
-
#
|
|
557
|
-
#
|
|
558
|
-
#
|
|
559
|
-
#
|
|
560
|
-
#
|
|
561
|
-
#
|
|
562
|
-
#
|
|
563
|
-
#
|
|
564
|
-
#
|
|
565
|
-
#
|
|
566
|
-
#
|
|
567
|
-
#
|
|
568
|
-
#
|
|
569
|
-
#
|
|
570
|
-
#
|
|
139
|
+
# def initialize( code, name, desc, cost )
|
|
140
|
+
# @code = code
|
|
141
|
+
# @name = name
|
|
142
|
+
# @desc = desc
|
|
143
|
+
# @cost = cost
|
|
144
|
+
#
|
|
145
|
+
# @features = [ ]
|
|
146
|
+
# end
|
|
147
|
+
#
|
|
148
|
+
# def add_feature( feature )
|
|
149
|
+
# @features << feature
|
|
150
|
+
# end
|
|
151
|
+
#
|
|
152
|
+
# # Support templating of member data.
|
|
153
|
+
# def get_binding
|
|
154
|
+
# binding
|
|
155
|
+
# end
|
|
156
|
+
#
|
|
157
|
+
# # ...
|
|
158
|
+
# end
|
|
159
|
+
#
|
|
160
|
+
# # Create template.
|
|
161
|
+
# template = %{
|
|
162
|
+
# <html>
|
|
163
|
+
# <head><title>Ruby Toys -- <%= @name %></title></head>
|
|
164
|
+
# <body>
|
|
165
|
+
#
|
|
166
|
+
# <h1><%= @name %> (<%= @code %>)</h1>
|
|
167
|
+
# <p><%= @desc %></p>
|
|
168
|
+
#
|
|
169
|
+
# <ul>
|
|
170
|
+
# <% @features.each do |f| %>
|
|
171
|
+
# <li><b><%= f %></b></li>
|
|
172
|
+
# <% end %>
|
|
173
|
+
# </ul>
|
|
174
|
+
#
|
|
175
|
+
# <p>
|
|
176
|
+
# <% if @cost < 10 %>
|
|
177
|
+
# <b>Only <%= @cost %>!!!</b>
|
|
178
|
+
# <% else %>
|
|
179
|
+
# Call for a price, today!
|
|
180
|
+
# <% end %>
|
|
181
|
+
# </p>
|
|
182
|
+
#
|
|
183
|
+
# </body>
|
|
184
|
+
# </html>
|
|
185
|
+
# }.gsub(/^ /, '')
|
|
186
|
+
#
|
|
187
|
+
# rhtml = ERB.new(template)
|
|
188
|
+
#
|
|
189
|
+
# # Set up template data.
|
|
190
|
+
# toy = Product.new( "TZ-1002",
|
|
191
|
+
# "Rubysapien",
|
|
192
|
+
# "Geek's Best Friend! Responds to Ruby commands...",
|
|
193
|
+
# 999.95 )
|
|
194
|
+
# toy.add_feature("Listens for verbal commands in the Ruby language!")
|
|
195
|
+
# toy.add_feature("Ignores Perl, Java, and all C variants.")
|
|
196
|
+
# toy.add_feature("Karate-Chop Action!!!")
|
|
197
|
+
# toy.add_feature("Matz signature on left leg.")
|
|
198
|
+
# toy.add_feature("Gem studded eyes... Rubies, of course!")
|
|
199
|
+
#
|
|
200
|
+
# # Produce result.
|
|
201
|
+
# rhtml.run(toy.get_binding)
|
|
202
|
+
#
|
|
203
|
+
# *Generates (some blank lines removed):*
|
|
204
|
+
#
|
|
205
|
+
# <html>
|
|
206
|
+
# <head><title>Ruby Toys -- Rubysapien</title></head>
|
|
207
|
+
# <body>
|
|
208
|
+
#
|
|
209
|
+
# <h1>Rubysapien (TZ-1002)</h1>
|
|
210
|
+
# <p>Geek's Best Friend! Responds to Ruby commands...</p>
|
|
211
|
+
#
|
|
212
|
+
# <ul>
|
|
213
|
+
# <li><b>Listens for verbal commands in the Ruby language!</b></li>
|
|
214
|
+
# <li><b>Ignores Perl, Java, and all C variants.</b></li>
|
|
215
|
+
# <li><b>Karate-Chop Action!!!</b></li>
|
|
216
|
+
# <li><b>Matz signature on left leg.</b></li>
|
|
217
|
+
# <li><b>Gem studded eyes... Rubies, of course!</b></li>
|
|
218
|
+
# </ul>
|
|
219
|
+
#
|
|
220
|
+
# <p>
|
|
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.
|
|
571
236
|
#
|
|
572
237
|
class ERB
|
|
573
238
|
# <!--
|
|
574
239
|
# rdoc-file=lib/erb.rb
|
|
575
|
-
# -
|
|
240
|
+
# - version()
|
|
576
241
|
# -->
|
|
577
|
-
# Returns
|
|
242
|
+
# Returns revision information for the erb.rb module.
|
|
578
243
|
#
|
|
579
244
|
def self.version: () -> String
|
|
580
245
|
|
|
581
246
|
# <!--
|
|
582
247
|
# rdoc-file=lib/erb.rb
|
|
583
|
-
# -
|
|
248
|
+
# - new(str, safe_level=NOT_GIVEN, legacy_trim_mode=NOT_GIVEN, legacy_eoutvar=NOT_GIVEN, trim_mode: nil, eoutvar: '_erbout')
|
|
584
249
|
# -->
|
|
585
|
-
#
|
|
586
|
-
#
|
|
587
|
-
# ERB
|
|
588
|
-
#
|
|
589
|
-
#
|
|
590
|
-
#
|
|
591
|
-
#
|
|
592
|
-
#
|
|
593
|
-
#
|
|
594
|
-
#
|
|
595
|
-
#
|
|
596
|
-
#
|
|
597
|
-
#
|
|
598
|
-
# *
|
|
599
|
-
#
|
|
600
|
-
#
|
|
601
|
-
#
|
|
602
|
-
#
|
|
603
|
-
#
|
|
604
|
-
#
|
|
605
|
-
#
|
|
606
|
-
#
|
|
607
|
-
#
|
|
608
|
-
#
|
|
609
|
-
#
|
|
610
|
-
#
|
|
250
|
+
# Constructs a new ERB object with the template specified in *str*.
|
|
251
|
+
#
|
|
252
|
+
# An ERB object works by building a chunk of Ruby code that will output the
|
|
253
|
+
# completed template when run.
|
|
254
|
+
#
|
|
255
|
+
# If *trim_mode* is passed a String containing one or more of the following
|
|
256
|
+
# modifiers, ERB will adjust its code generation as listed:
|
|
257
|
+
#
|
|
258
|
+
# % enables Ruby code processing for lines beginning with %
|
|
259
|
+
# <> omit newline for lines starting with <% and ending in %>
|
|
260
|
+
# > omit newline for lines ending in %>
|
|
261
|
+
# - omit blank lines ending in -%>
|
|
262
|
+
#
|
|
263
|
+
# *eoutvar* can be used to set the name of the variable ERB will build up its
|
|
264
|
+
# output in. This is useful when you need to run multiple ERB templates through
|
|
265
|
+
# the same binding and/or when you want to control where output ends up. Pass
|
|
266
|
+
# the name of the variable to be used inside a String.
|
|
267
|
+
#
|
|
268
|
+
# ### Example
|
|
269
|
+
#
|
|
270
|
+
# require "erb"
|
|
271
|
+
#
|
|
272
|
+
# # build data class
|
|
273
|
+
# class Listings
|
|
274
|
+
# PRODUCT = { :name => "Chicken Fried Steak",
|
|
275
|
+
# :desc => "A well messages pattie, breaded and fried.",
|
|
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.
|
|
611
312
|
#
|
|
612
313
|
def initialize: (String, ?eoutvar: String, ?trim_mode: Integer | String | NilClass) -> untyped
|
|
613
314
|
|
|
614
315
|
# <!-- rdoc-file=lib/erb.rb -->
|
|
615
|
-
#
|
|
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
|
|
316
|
+
# The Ruby code generated by ERB
|
|
645
317
|
#
|
|
646
318
|
def src: () -> String
|
|
647
319
|
|
|
648
320
|
# <!-- rdoc-file=lib/erb.rb -->
|
|
649
|
-
#
|
|
650
|
-
# see [Encodings](rdoc-ref:ERB@Encodings):
|
|
321
|
+
# The encoding to eval
|
|
651
322
|
#
|
|
652
323
|
def encoding: () -> Encoding
|
|
653
324
|
|
|
654
325
|
# <!-- rdoc-file=lib/erb.rb -->
|
|
655
|
-
#
|
|
656
|
-
#
|
|
326
|
+
# The optional *filename* argument passed to Kernel#eval when the ERB code is
|
|
327
|
+
# run
|
|
657
328
|
#
|
|
658
329
|
def filename: () -> (String | NilClass)
|
|
659
330
|
|
|
660
331
|
# <!-- rdoc-file=lib/erb.rb -->
|
|
661
|
-
#
|
|
662
|
-
#
|
|
332
|
+
# The optional *filename* argument passed to Kernel#eval when the ERB code is
|
|
333
|
+
# run
|
|
663
334
|
#
|
|
664
335
|
def filename=: (String | NilClass) -> untyped
|
|
665
336
|
|
|
666
337
|
# <!-- rdoc-file=lib/erb.rb -->
|
|
667
|
-
#
|
|
668
|
-
# see [Error Reporting](rdoc-ref:ERB@Error+Reporting).
|
|
338
|
+
# The optional *lineno* argument passed to Kernel#eval when the ERB code is run
|
|
669
339
|
#
|
|
670
340
|
def lineno: () -> Integer
|
|
671
341
|
|
|
672
342
|
# <!-- rdoc-file=lib/erb.rb -->
|
|
673
|
-
#
|
|
674
|
-
# see [Error Reporting](rdoc-ref:ERB@Error+Reporting).
|
|
343
|
+
# The optional *lineno* argument passed to Kernel#eval when the ERB code is run
|
|
675
344
|
#
|
|
676
345
|
def lineno=: (Integer) -> untyped
|
|
677
346
|
|
|
678
347
|
# <!--
|
|
679
348
|
# rdoc-file=lib/erb.rb
|
|
680
|
-
# - location
|
|
681
|
-
# - location = filename -> filename
|
|
349
|
+
# - location=((filename, lineno))
|
|
682
350
|
# -->
|
|
683
|
-
# Sets
|
|
684
|
-
#
|
|
351
|
+
# Sets optional filename and line number that will be used in ERB code
|
|
352
|
+
# evaluation and error reporting. See also #filename= and #lineno=
|
|
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
|
|
685
364
|
#
|
|
686
365
|
def location=: (Array[String | Integer]) -> untyped
|
|
687
366
|
|
|
688
367
|
# <!--
|
|
689
368
|
# rdoc-file=lib/erb.rb
|
|
690
|
-
# - run(
|
|
369
|
+
# - run(b=new_toplevel)
|
|
691
370
|
# -->
|
|
692
|
-
#
|
|
693
|
-
# returns `nil`.
|
|
371
|
+
# Generate results and print them. (see ERB#result)
|
|
694
372
|
#
|
|
695
373
|
def run: (?Binding) -> untyped
|
|
696
374
|
|
|
697
375
|
# <!--
|
|
698
376
|
# rdoc-file=lib/erb.rb
|
|
699
|
-
# - result(
|
|
377
|
+
# - result(b=new_toplevel)
|
|
700
378
|
# -->
|
|
701
|
-
#
|
|
702
|
-
#
|
|
703
|
-
#
|
|
704
|
-
#
|
|
705
|
-
#
|
|
706
|
-
#
|
|
707
|
-
# See also #result_with_hash.
|
|
379
|
+
# Executes the generated ERB code to produce a completed template, returning the
|
|
380
|
+
# results of that code. (See ERB::new for details on how this process can be
|
|
381
|
+
# affected by *safe_level*.)
|
|
382
|
+
#
|
|
383
|
+
# *b* accepts a Binding object which is used to set the context of code
|
|
384
|
+
# evaluation.
|
|
708
385
|
#
|
|
709
386
|
def result: (?Binding) -> String
|
|
710
387
|
|
|
711
388
|
# <!--
|
|
712
389
|
# rdoc-file=lib/erb.rb
|
|
713
|
-
# - result_with_hash(hash)
|
|
390
|
+
# - result_with_hash(hash)
|
|
714
391
|
# -->
|
|
715
|
-
#
|
|
716
|
-
#
|
|
717
|
-
# see [Augmented Binding](rdoc-ref:ERB@Augmented+Binding).
|
|
718
|
-
# See also #result.
|
|
392
|
+
# Render a template on a new toplevel binding with local variables specified by
|
|
393
|
+
# a Hash object.
|
|
719
394
|
#
|
|
720
395
|
def result_with_hash: (Hash[untyped, untyped]) -> String
|
|
721
396
|
|
|
722
397
|
# <!--
|
|
723
398
|
# rdoc-file=lib/erb.rb
|
|
724
|
-
# - def_method(
|
|
399
|
+
# - def_method(mod, methodname, fname='(ERB)')
|
|
725
400
|
# -->
|
|
726
|
-
#
|
|
727
|
-
#
|
|
728
|
-
#
|
|
729
|
-
#
|
|
730
|
-
#
|
|
731
|
-
#
|
|
732
|
-
#
|
|
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"
|
|
401
|
+
# Define *methodname* as instance method of *mod* from compiled Ruby source.
|
|
402
|
+
#
|
|
403
|
+
# example:
|
|
404
|
+
# filename = 'example.rhtml' # 'arg1' and 'arg2' are used in example.rhtml
|
|
405
|
+
# erb = ERB.new(File.read(filename))
|
|
406
|
+
# erb.def_method(MyClass, 'render(arg1, arg2)', filename)
|
|
407
|
+
# print MyClass.new.render('foo', 123)
|
|
738
408
|
#
|
|
739
409
|
def def_method: (Module, String, ?String) -> untyped
|
|
740
410
|
|
|
741
411
|
# <!--
|
|
742
412
|
# rdoc-file=lib/erb.rb
|
|
743
|
-
# - def_module(
|
|
413
|
+
# - def_module(methodname='erb')
|
|
744
414
|
# -->
|
|
745
|
-
#
|
|
746
|
-
#
|
|
747
|
-
#
|
|
748
|
-
#
|
|
749
|
-
#
|
|
750
|
-
#
|
|
751
|
-
#
|
|
752
|
-
#
|
|
753
|
-
#
|
|
415
|
+
# Create unnamed module, define *methodname* as instance method of it, and
|
|
416
|
+
# return it.
|
|
417
|
+
#
|
|
418
|
+
# example:
|
|
419
|
+
# filename = 'example.rhtml' # 'arg1' and 'arg2' are used in example.rhtml
|
|
420
|
+
# erb = ERB.new(File.read(filename))
|
|
421
|
+
# erb.filename = filename
|
|
422
|
+
# MyModule = erb.def_module('render(arg1, arg2)')
|
|
423
|
+
# class MyClass
|
|
424
|
+
# include MyModule
|
|
425
|
+
# end
|
|
754
426
|
#
|
|
755
427
|
def def_module: (?String) -> Module
|
|
756
428
|
|
|
757
429
|
# <!--
|
|
758
430
|
# rdoc-file=lib/erb.rb
|
|
759
|
-
# - def_class(
|
|
431
|
+
# - def_class(superklass=Object, methodname='result')
|
|
760
432
|
# -->
|
|
761
|
-
#
|
|
762
|
-
#
|
|
763
|
-
#
|
|
764
|
-
#
|
|
765
|
-
#
|
|
766
|
-
#
|
|
767
|
-
#
|
|
768
|
-
#
|
|
769
|
-
#
|
|
770
|
-
#
|
|
771
|
-
#
|
|
772
|
-
#
|
|
773
|
-
#
|
|
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>
|
|
433
|
+
# Define unnamed class which has *methodname* as instance method, and return it.
|
|
434
|
+
#
|
|
435
|
+
# example:
|
|
436
|
+
# class MyClass_
|
|
437
|
+
# def initialize(arg1, arg2)
|
|
438
|
+
# @arg1 = arg1; @arg2 = arg2
|
|
439
|
+
# end
|
|
440
|
+
# end
|
|
441
|
+
# filename = 'example.rhtml' # @arg1 and @arg2 are used in example.rhtml
|
|
442
|
+
# erb = ERB.new(File.read(filename))
|
|
443
|
+
# erb.filename = filename
|
|
444
|
+
# MyClass = erb.def_class(MyClass_, 'render()')
|
|
445
|
+
# print MyClass.new('foo', 123).render()
|
|
794
446
|
#
|
|
795
447
|
def def_class: (?Class, ?String) -> Class
|
|
796
448
|
|
|
797
|
-
# <!-- rdoc-file=lib/erb/util.rb -->
|
|
798
|
-
# ERB::Util
|
|
799
|
-
#
|
|
800
|
-
# A utility module for conversion routines, often handy in HTML generation.
|
|
801
|
-
#
|
|
802
449
|
module Util
|
|
803
450
|
# <!--
|
|
804
451
|
# rdoc-file=lib/erb.rb
|
|
@@ -863,37 +510,6 @@ class ERB
|
|
|
863
510
|
alias self.u self.url_encode
|
|
864
511
|
end
|
|
865
512
|
|
|
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
|
-
#
|
|
897
513
|
module DefMethod
|
|
898
514
|
# <!--
|
|
899
515
|
# rdoc-file=lib/erb/def_method.rb
|
|
@@ -905,12 +521,6 @@ class ERB
|
|
|
905
521
|
def self.def_erb_method: (String methodname, (String | ERB) erb_or_fname) -> untyped
|
|
906
522
|
end
|
|
907
523
|
|
|
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
|
-
#
|
|
914
524
|
module Escape
|
|
915
525
|
# <!--
|
|
916
526
|
# rdoc-file=lib/erb/util.rb
|