diff --git a/NEWS.md b/NEWS.md index 0e1ecf093f4381..08c75c1a679085 100644 --- a/NEWS.md +++ b/NEWS.md @@ -119,7 +119,7 @@ releases. * 4.0.3 to [v4.0.4][RubyGems-v4.0.4], [v4.0.5][RubyGems-v4.0.5], [v4.0.6][RubyGems-v4.0.6], [v4.0.7][RubyGems-v4.0.7], [v4.0.8][RubyGems-v4.0.8], [v4.0.9][RubyGems-v4.0.9], [v4.0.10][RubyGems-v4.0.10], [v4.0.11][RubyGems-v4.0.11], [v4.0.12][RubyGems-v4.0.12], [v4.0.13][RubyGems-v4.0.13], [v4.0.14][RubyGems-v4.0.14], [v4.0.15][RubyGems-v4.0.15], [v4.0.16][RubyGems-v4.0.16] * bundler 4.1.0.dev * 4.0.3 to [v4.0.4][bundler-v4.0.4], [v4.0.5][bundler-v4.0.5], [v4.0.6][bundler-v4.0.6], [v4.0.7][bundler-v4.0.7], [v4.0.8][bundler-v4.0.8], [v4.0.9][bundler-v4.0.9], [v4.0.10][bundler-v4.0.10], [v4.0.11][bundler-v4.0.11], [v4.0.12][bundler-v4.0.12], [v4.0.13][bundler-v4.0.13], [v4.0.14][bundler-v4.0.14], [v4.0.15][bundler-v4.0.15], [v4.0.16][bundler-v4.0.16] -* erb 6.0.5 +* erb 6.0.6 * 6.0.1 to [v6.0.1.1][erb-v6.0.1.1], [v6.0.2][erb-v6.0.2], [v6.0.3][erb-v6.0.3], [v6.0.4][erb-v6.0.4], [v6.0.5][erb-v6.0.5] * error_highlight 0.7.2 * ipaddr 1.2.9 diff --git a/lib/erb.rb b/lib/erb.rb index a4b481a860eaec..0c4653d291f28d 100644 --- a/lib/erb.rb +++ b/lib/erb.rb @@ -53,7 +53,7 @@ # # Here's how \ERB works: # -# - You can create a *template*: a plain-text string that includes specially formatted *tags*.. +# - You can create a *template*: a plain-text string that includes specially formatted *tags*. # - You can create an \ERB object to store the template. # - You can call instance method ERB#result to get the *result*. # @@ -63,10 +63,12 @@ # each begins with `'<%='`, ends with `'%>'`; contains a Ruby expression; # in the result, the value of the expression replaces the entire tag: # -# template = 'The magic word is <%= magic_word %>.' -# erb = ERB.new(template) -# magic_word = 'xyzzy' -# erb.result(binding) # => "The magic word is xyzzy." +# ``` +# template = 'The magic word is <%= magic_word %>.' +# erb = ERB.new(template) +# magic_word = 'xyzzy' +# erb.result(binding) # => "The magic word is xyzzy." +# ``` # # The above call to #result passes argument `binding`, # which contains the binding of variable `magic_word` to its string value `'xyzzy'`. @@ -74,21 +76,27 @@ # The below call to #result need not pass a binding, # because its expression `Date::DAYNAMES` is globally defined. # -# ERB.new('Today is <%= Date::DAYNAMES[Date.today.wday] %>.').result # => "Today is Monday." +# ``` +# ERB.new('Today is <%= Date::DAYNAMES[Date.today.wday] %>.').result # => "Today is Monday." +# ``` # # - [Execution tags][execution tags]: # each begins with `'<%'`, ends with `'%>'`; contains Ruby code to be executed: # -# template = '<% File.write("t.txt", "Some stuff.") %>' -# ERB.new(template).result -# File.read('t.txt') # => "Some stuff." +# ``` +# template = '<% File.write("t.txt", "Some stuff.") %>' +# ERB.new(template).result +# File.read('t.txt') # => "Some stuff." +# ``` # # - [Comment tags][comment tags]: # each begins with `'<%#'`, ends with `'%>'`; contains comment text; # in the result, the entire tag is omitted. # -# template = 'Some stuff;<%# Note to self: figure out what the stuff is. %> more stuff.' -# ERB.new(template).result # => "Some stuff; more stuff." +# ``` +# template = 'Some stuff;<%# Note to self: figure out what the stuff is. %> more stuff.' +# ERB.new(template).result # => "Some stuff; more stuff." +# ``` # # ## Some Simple Examples # @@ -106,11 +114,10 @@ # 1. A plain-text string is assigned to variable `template`. # Its embedded [expression tag][expression tags] `'<%= Time.now %>'` includes a Ruby expression, `Time.now`. # 2. The string is put into a new \ERB object, and stored in variable `erb`. -# 4. Method call `erb.result` generates a string that contains the run-time value of `Time.now`, +# 3. Method call `erb.result` generates a string that contains the run-time value of `Time.now`, # as computed at the time of the call. # -# The -# \ERB object may be re-used: +# The \ERB object may be re-used: # # ``` # erb.result @@ -148,14 +155,14 @@ # ## Bindings # # A call to method #result, which produces the formatted result string, -# requires a [Binding object][binding object] as its argument. +# requires a [`Binding` object][binding object] as its argument. # # The binding object provides the bindings for expressions in [expression tags][expression tags]. # # There are three ways to provide the required binding: # -# - [Default binding][default binding]. -# - [Local binding][local binding]. +# - [Default binding][default binding] +# - [Local binding][local binding] # - [Augmented binding][augmented binding] # # ### Default Binding @@ -174,6 +181,9 @@ # The current process is <%= $0 %>. # TEMPLATE # puts ERB.new(template).result +# ``` +# +# ``` # The Ruby copyright is "ruby - Copyright (C) 1993-2025 Yukihiro Matsumoto". # The current process is irb. # ``` @@ -183,7 +193,7 @@ # ### Local Binding # # The default binding is *not* sufficient for an expression -# that refers to a a constant or variable that is not defined there: +# that refers to a constant or variable that is not defined there: # # ``` # Foo = 1 # Defines local constant Foo. @@ -209,6 +219,9 @@ # # ``` # puts erb.result(binding) +# ``` +# +# ``` # The current value of constant Foo is 1. # The current value of variable foo is 2. # The Ruby copyright is "ruby - Copyright (C) 1993-2025 Yukihiro Matsumoto". @@ -246,6 +259,9 @@ # ``` # hash = {bar: 3, baz: 4} # puts erb.result_with_hash(hash) +# ``` +# +# ``` # The current value of variable bar is 3. # The current value of variable baz is 4. # The Ruby copyright is "ruby - Copyright (C) 1993-2025 Yukihiro Matsumoto". @@ -268,7 +284,7 @@ # # You can embed a Ruby expression in a template using an *expression tag*. # -# Its syntax is `<%= _expression_ %>`, +# Its syntax is `<%= expression %>`, # where *expression* is any valid Ruby expression. # # When you call method #result, @@ -298,7 +314,7 @@ # # You can embed Ruby executable code in template using an *execution tag*. # -# Its syntax is `<% _code_ %>`, +# Its syntax is `<% code %>`, # where *code* is any valid Ruby code. # # When you call method #result, @@ -306,17 +322,17 @@ # (generating no text in the result): # # ``` -# ERB.new('foo <% Dir.chdir("C:/") %> bar').result # => "foo bar" +# ERB.new('foo <% Dir.chdir(Dir.pwd) %> bar').result # => "foo bar" # ``` # # Whitespace before and after the embedded code is optional: # # ``` -# ERB.new('foo <%Dir.chdir("C:/")%> bar').result # => "foo bar" +# ERB.new('foo <%Dir.chdir(Dir.pwd)%> bar').result # => "foo bar" # ``` # # You can interleave text with execution tags to form a control structure -# such as a conditional, a loop, or a `case` statements. +# such as a conditional, a loop, or a `case` statement. # # Conditional: # @@ -386,7 +402,7 @@ # #### Shorthand Format for Execution Tags # # You can use keyword argument `trim_mode: '%'` to enable a shorthand format for execution tags; -# this example uses the shorthand format `% _code_` instead of `<% _code_ %>`: +# this example uses the shorthand format `% code` instead of `<% code %>`: # # ``` # template = <