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