Skip to content

Commit 5cfd49f

Browse files
BurdetteLamarmatzbot
authored andcommitted
[ruby/erb] [DOC] Enhanced doc for ERB.new
(ruby/erb#68) ruby/erb@9591b5d23b
1 parent 5e90d9e commit 5cfd49f

File tree

1 file changed

+42
-50
lines changed

1 file changed

+42
-50
lines changed

lib/erb.rb

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,9 @@
259259
# # => "\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"
260260
# ```
261261
#
262-
# You can use keyword argument `trim_mode` to make certain adjustments to the processing;
263-
# see ERB.new.
262+
# #### Shorthand Format for Execution Tags
264263
#
265-
# In particular, you can give `trim_mode: '%'` to enable a shorthand format for execution tags;
264+
# You can give `trim_mode: '%'` to enable a shorthand format for execution tags;
266265
# this example uses the shorthand format `% _code_` instead of `<% _code_ %>`:
267266
#
268267
# ```
@@ -527,69 +526,62 @@ def self.version
527526
VERSION
528527
end
529528

529+
# :markup: markdown
530530
#
531-
# Constructs a new ERB object with the template specified in _str_.
531+
# :call-seq:
532+
# ERB.new(string, trim_mode: nil, eoutvar: '_erbout')
532533
#
533-
# An ERB object works by building a chunk of Ruby code that will output
534-
# the completed template when run.
534+
# Returns a new \ERB object containing the given +string+.
535535
#
536-
# If _trim_mode_ is passed a String containing one or more of the following
537-
# modifiers, ERB will adjust its code generation as listed:
536+
# For details about `string`, its embedded tags, and generated results, see ERB.
538537
#
539-
# % enables Ruby code processing for lines beginning with %
540-
# <> omit newline for lines starting with <% and ending in %>
541-
# > omit newline for lines ending in %>
542-
# - omit blank lines ending in -%>
538+
# **Keyword Argument `trim_mode`**
543539
#
544-
# _eoutvar_ can be used to set the name of the variable ERB will build up
545-
# its output in. This is useful when you need to run multiple ERB
546-
# templates through the same binding and/or when you want to control where
547-
# output ends up. Pass the name of the variable to be used inside a String.
540+
# When keyword argument `trim_mode` has a string value,
541+
# that value may be one of:
548542
#
549-
# ### Example
543+
# - `'%'`: Enable [shorthand format][shorthand format] for execution tags.
544+
# - `'-'`: Omit each blank line ending with `'%>'`.
545+
# - `'>'`: Omit newline for each line ending with `'%>'`.
546+
# - `'<>'`: Omit newline for each line starting with `'<%'` and ending with `'%>'`.
550547
#
551-
# require "erb"
548+
# The value may also be certain combinations of the above.
552549
#
553-
# # build data class
554-
# class Listings
555-
# PRODUCT = { :name => "Chicken Fried Steak",
556-
# :desc => "A well messaged pattie, breaded and fried.",
557-
# :cost => 9.95 }
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 `'%>'`.
558553
#
559-
# attr_reader :product, :price
554+
# **Keyword Argument `eoutvar`**
560555
#
561-
# def initialize( product = "", price = "" )
562-
# @product = product
563-
# @price = price
564-
# end
556+
# The string value of keyword argument `eoutvar` specifies the name of the variable
557+
# that method #result uses to construct its result string.
558+
# This is useful when you need to run multiple \ERB templates through the same binding
559+
# and/or when you want to control where output ends up.
565560
#
566-
# def build
567-
# b = binding
568-
# # create and run templates, filling member data variables
569-
# ERB.new(<<~'END_PRODUCT', trim_mode: "", eoutvar: "@product").result b
570-
# <%= PRODUCT[:name] %>
571-
# <%= PRODUCT[:desc] %>
572-
# END_PRODUCT
573-
# ERB.new(<<~'END_PRICE', trim_mode: "", eoutvar: "@price").result b
574-
# <%= PRODUCT[:name] %> -- <%= PRODUCT[:cost] %>
575-
# <%= PRODUCT[:desc] %>
576-
# END_PRICE
577-
# end
578-
# end
561+
# It's good practice to choose a variable name that begins with an underscore: `'_'`.
579562
#
580-
# # setup template data
581-
# listings = Listings.new
582-
# listings.build
563+
# <b>Backward Compatibility</b>
583564
#
584-
# puts listings.product + "\n" + listings.price
565+
# The calling sequence given above -- which is the one you should use --
566+
# is a simplified version of the complete formal calling sequence,
567+
# which is:
585568
#
586-
# _Generates_
569+
# ```
570+
# ERB.new(string,
571+
# safe_level=NOT_GIVEN, legacy_trim_mode=NOT_GIVEN, legacy_eoutvar=NOT_GIVEN,
572+
# trim_mode: nil, eoutvar: '_erbout')
573+
# ```
587574
#
588-
# Chicken Fried Steak
589-
# A well massaged pattie, breaded and fried.
575+
# The second, third, and fourth positional arguments (those in the second line above) are deprecated;
576+
# this method issues warnings if they are given.
590577
#
591-
# Chicken Fried Steak -- 9.95
592-
# A well massaged pattie, breaded and fried.
578+
# However, their values, if given, are handled thus:
579+
#
580+
# - `safe_level`: ignored.
581+
# - `legacy_trim_mode: overrides keyword argument `trim_mode`.
582+
# - `legacy_eoutvar: overrides keyword argument `eoutvar`.
583+
#
584+
# [shorthand format]: rdoc-ref:ERB@Shorthand+Format+for+Execution+Tags
593585
#
594586
def initialize(str, safe_level=NOT_GIVEN, legacy_trim_mode=NOT_GIVEN, legacy_eoutvar=NOT_GIVEN, trim_mode: nil, eoutvar: '_erbout')
595587
# Complex initializer for $SAFE deprecation at [Feature #14256]. Use keyword arguments to pass trim_mode or eoutvar.

0 commit comments

Comments
 (0)