Skip to content

Commit 061f9b8

Browse files
BurdetteLamarmatzbot
authored andcommitted
[ruby/erb] [DOC] More on class ERB
(ruby/erb#69) * [DOC] More on class �ERB * [DOC] More on class �ERB * More * More * More ruby/erb@d9d73ed58e
1 parent 7dd9c76 commit 061f9b8

1 file changed

Lines changed: 109 additions & 12 deletions

File tree

lib/erb.rb

Lines changed: 109 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,17 @@
189189
#
190190
# When you call method #result,
191191
# the method executes the code and removes the entire execution tag
192-
# (generating no text in the result).
192+
# (generating no text in the result):
193+
#
194+
# ```
195+
# ERB.new('foo <% Dir.chdir("C:/") %> bar').result # => "foo bar"
196+
# ```
197+
#
198+
# Whitespace before and after the embedded code is optional:
199+
#
200+
# ```
201+
# ERB.new('foo <%Dir.chdir("C:/")%> bar').result # => "foo bar"
202+
# ```
193203
#
194204
# You can interleave text with execution tags to form a control structure
195205
# such as a conditional, a loop, or a `case` statements.
@@ -261,7 +271,7 @@
261271
#
262272
# #### Shorthand Format for Execution Tags
263273
#
264-
# You can give `trim_mode: '%'` to enable a shorthand format for execution tags;
274+
# You can use keyword argument `trim_mode: '%'` to enable a shorthand format for execution tags;
265275
# this example uses the shorthand format `% _code_` instead of `<% _code_ %>`:
266276
#
267277
# ```
@@ -283,6 +293,90 @@
283293
# Note that in the shorthand format, the character `'%'` must be the first character in the code line
284294
# (no leading whitespace).
285295
#
296+
# #### Suppressing Unwanted Blank Lines
297+
#
298+
# With keyword argument `trim_mode` not given,
299+
# all blank lines go into the result:
300+
#
301+
# ```
302+
# s = <<EOT
303+
# <% if true %>
304+
# <%= RUBY_VERSION %>
305+
# <% end %>
306+
# EOT
307+
# ERB.new(s).result.lines.each {|line| puts line.inspect }
308+
# "\n"
309+
# "3.4.5\n"
310+
# "\n"
311+
# ```
312+
#
313+
# You can give `trim_mode: '-'`, you can suppress each blank line
314+
# whose source line ends with `-%>` (instead of `%>`):
315+
#
316+
# ```
317+
# s = <<EOT
318+
# <% if true -%>
319+
# <%= RUBY_VERSION %>
320+
# <% end -%>
321+
# EOT
322+
# ERB.new(s, trim_mode: '-').result.lines.each {|line| puts line.inspect }
323+
# "3.4.5\n"
324+
# ```
325+
#
326+
# It is an error to use the trailing `'-%>'` notation without `trim_mode: '-'`:
327+
#
328+
# ```
329+
# ERB.new(s).result.lines.each {|line| puts line.inspect } # Raises SyntaxError.
330+
# ```
331+
#
332+
# #### Suppressing Unwanted Newlines
333+
#
334+
# Consider this input string:
335+
#
336+
# ```
337+
# s = <<EOT
338+
# <% RUBY_VERSION %>
339+
# <%= RUBY_VERSION %>
340+
# foo <% RUBY_VERSION %>
341+
# foo <%= RUBY_VERSION %>
342+
# EOT
343+
# ```
344+
#
345+
# With keyword argument `trim_mode` not given, all newlines go into the result:
346+
#
347+
# ```
348+
# ERB.new(s).result.lines.each {|line| puts line.inspect }
349+
# "\n"
350+
# "3.4.5\n"
351+
# "foo \n"
352+
# "foo 3.4.5\n"
353+
# ```
354+
#
355+
# You can give `trim_mode: '>'` to suppress the trailing newline
356+
# for each line that ends with `'%<'` (regardless of its beginning):
357+
#
358+
# ```
359+
# ERB.new(s, trim_mode: '>').result.lines.each {|line| puts line.inspect }
360+
# "3.4.5foo foo 3.4.5"
361+
# ```
362+
#
363+
# You can give `trim_mode: '<>'` to suppress the trailing newline
364+
# for each line that both begins with `'<%'` and ends with `'%<'`:
365+
#
366+
# ```
367+
# ERB.new(s, trim_mode: '<>').result.lines.each {|line| puts line.inspect }
368+
# "3.4.5foo \n"
369+
# "foo 3.4.5\n"
370+
# ```
371+
#
372+
# #### Combining Trim Modes
373+
#
374+
# You can combine certain trim modes:
375+
#
376+
# - `'%-'`: Enable shorthand and omit each blank line ending with `'%>'`.
377+
# - `'%>'`: Enable shorthand and omit newline for each line ending with `'%>'`.
378+
# - `'%<>'`: Enable shorthand and omit newline for each line starting with `'<%'` and ending with `'%>'`.
379+
#
286380
# ### Comment Tags
287381
#
288382
# You can embed a comment in a template using a *comment tag*;
@@ -537,19 +631,19 @@ def self.version
537631
#
538632
# **Keyword Argument `trim_mode`**
539633
#
540-
# When keyword argument `trim_mode` has a string value,
541-
# that value may be one of:
634+
# You can use keyword argument `trim_mode: '%'`
635+
# to enable the [shorthand format][shorthand format] for execution tags.
636+
#
637+
# This value allows [blank line control][blank line control]:
542638
#
543-
# - `'%'`: Enable [shorthand format][shorthand format] for execution tags.
544639
# - `'-'`: Omit each blank line ending with `'%>'`.
640+
#
641+
# Other values allow [newline control][newline control]:
642+
#
545643
# - `'>'`: Omit newline for each line ending with `'%>'`.
546644
# - `'<>'`: Omit newline for each line starting with `'<%'` and ending with `'%>'`.
547645
#
548-
# The value may also be certain combinations of the above.
549-
#
550-
# - `'%-'`: Enable shorthand and omit each blank line ending with `'%>'`.
551-
# - `'%>'`: Enable shorthand and omit newline for each line ending with `'%>'`.
552-
# - `'%<>'`: Enable shorthand and omit newline for each line starting with `'<%'` and ending with `'%>'`.
646+
# You can also [combine trim modes][combine trim modes].
553647
#
554648
# **Keyword Argument `eoutvar`**
555649
#
@@ -578,9 +672,12 @@ def self.version
578672
# However, their values, if given, are handled thus:
579673
#
580674
# - `safe_level`: ignored.
581-
# - `legacy_trim_mode: overrides keyword argument `trim_mode`.
582-
# - `legacy_eoutvar: overrides keyword argument `eoutvar`.
675+
# - `legacy_trim_mode`: overrides keyword argument `trim_mode`.
676+
# - `legacy_eoutvar`: overrides keyword argument `eoutvar`.
583677
#
678+
# [blank line control]: rdoc-ref:ERB@Suppressing+Unwanted+Blank+Lines
679+
# [combine trim modes]: rdoc-ref:ERB@Combining+Trim+Modes
680+
# [newline control]: rdoc-ref:ERB@Suppressing+Unwanted+Newlines
584681
# [shorthand format]: rdoc-ref:ERB@Shorthand+Format+for+Execution+Tags
585682
#
586683
def initialize(str, safe_level=NOT_GIVEN, legacy_trim_mode=NOT_GIVEN, legacy_eoutvar=NOT_GIVEN, trim_mode: nil, eoutvar: '_erbout')

0 commit comments

Comments
 (0)