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.
Files changed (171) hide show
  1. checksums.yaml +4 -4
  2. data/.clang-format +74 -0
  3. data/.clangd +2 -0
  4. data/.github/workflows/c-check.yml +54 -0
  5. data/.github/workflows/comments.yml +3 -3
  6. data/.github/workflows/ruby.yml +34 -19
  7. data/.github/workflows/typecheck.yml +1 -1
  8. data/.github/workflows/windows.yml +1 -1
  9. data/.gitignore +4 -0
  10. data/README.md +38 -1
  11. data/Rakefile +152 -23
  12. data/config.yml +190 -62
  13. data/core/array.rbs +44 -43
  14. data/core/dir.rbs +2 -2
  15. data/core/encoding.rbs +3 -2
  16. data/core/enumerable.rbs +89 -2
  17. data/core/errno.rbs +8 -0
  18. data/core/errors.rbs +28 -1
  19. data/core/exception.rbs +2 -2
  20. data/core/fiber.rbs +3 -3
  21. data/core/file.rbs +26 -11
  22. data/core/float.rbs +1 -1
  23. data/core/gc.rbs +422 -281
  24. data/core/hash.rbs +1024 -727
  25. data/core/io/wait.rbs +11 -33
  26. data/core/io.rbs +6 -4
  27. data/core/kernel.rbs +49 -43
  28. data/core/marshal.rbs +1 -1
  29. data/core/match_data.rbs +1 -1
  30. data/core/math.rbs +42 -3
  31. data/core/method.rbs +14 -6
  32. data/core/module.rbs +71 -11
  33. data/core/nil_class.rbs +3 -3
  34. data/core/numeric.rbs +8 -8
  35. data/core/object.rbs +3 -3
  36. data/core/object_space.rbs +13 -0
  37. data/{stdlib/pathname/0 → core}/pathname.rbs +253 -352
  38. data/core/proc.rbs +15 -8
  39. data/core/process.rbs +2 -2
  40. data/core/ractor.rbs +278 -437
  41. data/core/range.rbs +6 -7
  42. data/core/rbs/unnamed/argf.rbs +1 -1
  43. data/core/rbs/unnamed/env_class.rbs +1 -1
  44. data/core/rbs/unnamed/random.rbs +4 -2
  45. data/core/regexp.rbs +22 -17
  46. data/core/ruby_vm.rbs +6 -4
  47. data/core/rubygems/errors.rbs +3 -70
  48. data/core/rubygems/rubygems.rbs +11 -79
  49. data/core/set.rbs +439 -332
  50. data/core/string.rbs +2897 -1117
  51. data/core/struct.rbs +1 -1
  52. data/core/symbol.rbs +4 -4
  53. data/core/thread.rbs +83 -20
  54. data/core/time.rbs +35 -9
  55. data/core/unbound_method.rbs +14 -6
  56. data/docs/aliases.md +79 -0
  57. data/docs/collection.md +2 -2
  58. data/docs/gem.md +0 -1
  59. data/docs/sigs.md +3 -3
  60. data/ext/rbs_extension/ast_translation.c +1016 -0
  61. data/ext/rbs_extension/ast_translation.h +37 -0
  62. data/ext/rbs_extension/class_constants.c +157 -0
  63. data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +7 -1
  64. data/ext/rbs_extension/compat.h +10 -0
  65. data/ext/rbs_extension/extconf.rb +25 -1
  66. data/ext/rbs_extension/legacy_location.c +317 -0
  67. data/ext/rbs_extension/legacy_location.h +45 -0
  68. data/ext/rbs_extension/main.c +365 -14
  69. data/ext/rbs_extension/rbs_extension.h +6 -21
  70. data/ext/rbs_extension/rbs_string_bridging.c +9 -0
  71. data/ext/rbs_extension/rbs_string_bridging.h +24 -0
  72. data/include/rbs/ast.h +687 -0
  73. data/include/rbs/defines.h +86 -0
  74. data/include/rbs/lexer.h +199 -0
  75. data/include/rbs/location.h +59 -0
  76. data/include/rbs/parser.h +135 -0
  77. data/include/rbs/string.h +49 -0
  78. data/include/rbs/util/rbs_allocator.h +59 -0
  79. data/include/rbs/util/rbs_assert.h +20 -0
  80. data/include/rbs/util/rbs_buffer.h +83 -0
  81. data/include/rbs/util/rbs_constant_pool.h +6 -67
  82. data/include/rbs/util/rbs_encoding.h +282 -0
  83. data/include/rbs/util/rbs_unescape.h +23 -0
  84. data/include/rbs.h +1 -2
  85. data/lib/rbs/annotate/formatter.rb +3 -13
  86. data/lib/rbs/annotate/rdoc_annotator.rb +3 -1
  87. data/lib/rbs/annotate/rdoc_source.rb +1 -1
  88. data/lib/rbs/cli/validate.rb +2 -2
  89. data/lib/rbs/cli.rb +1 -1
  90. data/lib/rbs/collection/config/lockfile_generator.rb +1 -0
  91. data/lib/rbs/definition_builder/ancestor_builder.rb +5 -5
  92. data/lib/rbs/environment.rb +64 -59
  93. data/lib/rbs/environment_loader.rb +1 -1
  94. data/lib/rbs/errors.rb +1 -1
  95. data/lib/rbs/parser_aux.rb +5 -0
  96. data/lib/rbs/resolver/constant_resolver.rb +2 -2
  97. data/lib/rbs/resolver/type_name_resolver.rb +124 -38
  98. data/lib/rbs/test/type_check.rb +13 -0
  99. data/lib/rbs/types.rb +3 -1
  100. data/lib/rbs/version.rb +1 -1
  101. data/lib/rbs.rb +1 -1
  102. data/lib/rdoc/discover.rb +1 -1
  103. data/lib/rdoc_plugin/parser.rb +3 -3
  104. data/sig/annotate/formatter.rbs +2 -2
  105. data/sig/annotate/rdoc_annotater.rbs +1 -1
  106. data/sig/environment.rbs +57 -6
  107. data/sig/manifest.yaml +0 -1
  108. data/sig/parser.rbs +20 -0
  109. data/sig/resolver/type_name_resolver.rbs +38 -7
  110. data/sig/types.rbs +4 -1
  111. data/src/ast.c +1256 -0
  112. data/src/lexer.c +2956 -0
  113. data/src/lexer.re +147 -0
  114. data/src/lexstate.c +205 -0
  115. data/src/location.c +71 -0
  116. data/src/parser.c +3495 -0
  117. data/src/string.c +90 -0
  118. data/src/util/rbs_allocator.c +152 -0
  119. data/src/util/rbs_assert.c +21 -0
  120. data/src/util/rbs_buffer.c +54 -0
  121. data/src/util/rbs_constant_pool.c +16 -86
  122. data/src/util/rbs_encoding.c +21308 -0
  123. data/src/util/rbs_unescape.c +131 -0
  124. data/stdlib/cgi/0/core.rbs +2 -396
  125. data/stdlib/cgi/0/manifest.yaml +1 -0
  126. data/stdlib/cgi-escape/0/escape.rbs +153 -0
  127. data/stdlib/coverage/0/coverage.rbs +3 -1
  128. data/stdlib/delegate/0/delegator.rbs +10 -7
  129. data/stdlib/erb/0/erb.rbs +737 -347
  130. data/stdlib/fileutils/0/fileutils.rbs +18 -13
  131. data/stdlib/forwardable/0/forwardable.rbs +3 -0
  132. data/stdlib/json/0/json.rbs +67 -48
  133. data/stdlib/net-http/0/net-http.rbs +3 -0
  134. data/stdlib/objspace/0/objspace.rbs +8 -3
  135. data/stdlib/open-uri/0/open-uri.rbs +40 -0
  136. data/stdlib/openssl/0/openssl.rbs +182 -149
  137. data/stdlib/optparse/0/optparse.rbs +3 -3
  138. data/stdlib/rdoc/0/code_object.rbs +2 -2
  139. data/stdlib/rdoc/0/comment.rbs +2 -0
  140. data/stdlib/rdoc/0/options.rbs +76 -0
  141. data/stdlib/rdoc/0/rdoc.rbs +7 -5
  142. data/stdlib/rdoc/0/store.rbs +1 -1
  143. data/stdlib/resolv/0/resolv.rbs +25 -68
  144. data/stdlib/ripper/0/ripper.rbs +5 -2
  145. data/stdlib/singleton/0/singleton.rbs +3 -0
  146. data/stdlib/socket/0/socket.rbs +13 -1
  147. data/stdlib/socket/0/tcp_socket.rbs +10 -2
  148. data/stdlib/stringio/0/stringio.rbs +412 -80
  149. data/stdlib/strscan/0/string_scanner.rbs +31 -31
  150. data/stdlib/tempfile/0/tempfile.rbs +1 -1
  151. data/stdlib/tsort/0/cyclic.rbs +3 -0
  152. data/stdlib/uri/0/common.rbs +11 -2
  153. data/stdlib/uri/0/file.rbs +1 -1
  154. data/stdlib/uri/0/generic.rbs +16 -15
  155. data/stdlib/uri/0/rfc2396_parser.rbs +6 -7
  156. data/stdlib/zlib/0/zstream.rbs +1 -0
  157. metadata +41 -18
  158. data/ext/rbs_extension/lexer.c +0 -2728
  159. data/ext/rbs_extension/lexer.h +0 -179
  160. data/ext/rbs_extension/lexer.re +0 -147
  161. data/ext/rbs_extension/lexstate.c +0 -175
  162. data/ext/rbs_extension/location.c +0 -325
  163. data/ext/rbs_extension/location.h +0 -85
  164. data/ext/rbs_extension/parser.c +0 -2982
  165. data/ext/rbs_extension/parser.h +0 -18
  166. data/ext/rbs_extension/parserstate.c +0 -411
  167. data/ext/rbs_extension/parserstate.h +0 -163
  168. data/ext/rbs_extension/unescape.c +0 -32
  169. data/include/rbs/ruby_objs.h +0 -72
  170. data/src/constants.c +0 -153
  171. 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
- # # 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
- #
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
- # 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.
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
- # From: James Edward Gray II <james@grayproductions.net>
76
- # To: <%= to %>
77
- # Subject: Addressing Needs
78
- #
79
- # <%= to[/\w+/] %>:
433
+ # From: James Edward Gray II <james@grayproductions.net>
434
+ # To: <%= to %>
435
+ # Subject: Addressing Needs
80
436
  #
81
- # Just wanted to send a quick note assuring that your needs are being
82
- # addressed.
437
+ # <%= to[/\w+/] %>:
83
438
  #
84
- # I want you to know that my team will keep working on the issues,
85
- # especially:
439
+ # Just wanted to send a quick note assuring that your needs are being
440
+ # addressed.
86
441
  #
87
- # <%# ignore numerous minor requests -- focus on priorities %>
88
- # % priorities.each do |priority|
89
- # * <%= priority %>
90
- # % end
442
+ # I want you to know that my team will keep working on the issues,
443
+ # especially:
91
444
  #
92
- # Thanks for your patience.
445
+ # <%# ignore numerous minor requests -- focus on priorities %>
446
+ # % priorities.each do |priority|
447
+ # * <%= priority %>
448
+ # % end
93
449
  #
94
- # James Edward Gray II
95
- # }.gsub(/^ /, '')
450
+ # Thanks for your patience.
96
451
  #
97
- # message = ERB.new(template, trim_mode: "%<>")
452
+ # James Edward Gray II
453
+ # }
98
454
  #
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" ]
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
- # # Produce result.
106
- # email = message.result
107
- # puts email
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
- # *Generates:*
465
+ # From: James Edward Gray II <james@grayproductions.net>
466
+ # To: Community Spokesman <spokesman@ruby_community.org>
467
+ # Subject: Addressing Needs
110
468
  #
111
- # From: James Edward Gray II <james@grayproductions.net>
112
- # To: Community Spokesman <spokesman@ruby_community.org>
113
- # Subject: Addressing Needs
469
+ # Community:
114
470
  #
115
- # Community:
471
+ # Just wanted to send a quick note assuring that your needs are being
472
+ # addressed.
116
473
  #
117
- # Just wanted to send a quick note assuring that your needs are being addressed.
474
+ # I want you to know that my team will keep working on the issues,
475
+ # especially:
118
476
  #
119
- # I want you to know that my team will keep working on the issues, especially:
477
+ # * Run Ruby Quiz
478
+ # * Document Modules
479
+ # * Answer Questions on Ruby Talk
120
480
  #
121
- # * Run Ruby Quiz
122
- # * Document Modules
123
- # * Answer Questions on Ruby Talk
481
+ # Thanks for your patience.
124
482
  #
125
- # Thanks for your patience.
483
+ # James Edward Gray II
126
484
  #
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.
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
- # 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.
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 revision information for the erb.rb module.
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(str, safe_level=NOT_GIVEN, legacy_trim_mode=NOT_GIVEN, legacy_eoutvar=NOT_GIVEN, trim_mode: nil, eoutvar: '_erbout')
583
+ # - ERB.new(template, trim_mode: nil, eoutvar: '_erbout')
249
584
  # -->
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.
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
- # The Ruby code generated by ERB
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
- # The encoding to eval
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
- # The optional *filename* argument passed to Kernel#eval when the ERB code is
327
- # run
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
- # The optional *filename* argument passed to Kernel#eval when the ERB code is
333
- # run
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
- # The optional *lineno* argument passed to Kernel#eval when the ERB code is run
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
- # The optional *lineno* argument passed to Kernel#eval when the ERB code is run
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=((filename, lineno))
680
+ # - location = [filename, lineno] => [filename, lineno]
681
+ # - location = filename -> filename
350
682
  # -->
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
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(b=new_toplevel)
690
+ # - run(binding = new_toplevel) -> nil
370
691
  # -->
371
- # Generate results and print them. (see ERB#result)
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(b=new_toplevel)
699
+ # - result(binding = new_toplevel) -> new_string
378
700
  # -->
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.
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
- # Render a template on a new toplevel binding with local variables specified by
393
- # a Hash object.
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(mod, methodname, fname='(ERB)')
724
+ # - def_method(module, method_signature, filename = '(ERB)') -> method_name
400
725
  # -->
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)
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(methodname='erb')
743
+ # - def_module(method_name = 'erb') -> new_module
414
744
  # -->
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
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(superklass=Object, methodname='result')
759
+ # - def_class(super_class = Object, method_name = 'result') -> new_class
432
760
  # -->
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()
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