Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 67 additions & 29 deletions lib/erb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@
# Like method [sprintf][sprintf], \ERB can format run-time data into a string.
# \ERB, however,s is *much more powerful*.
#
# In simplest terms:
#
# - You can create an \ERB object to store a text *template* that includes specially formatted *tags*;
# each tag specifies data that at run-time is to replace the tag in the produced result.
# - You can call instance method ERB#result to get the result,
# which is the string formed by replacing each tag with run-time data.
#
# \ERB is commonly used to produce:
#
# - Customized or personalized email messages.
Expand All @@ -49,24 +42,41 @@
#
# ## In Brief
#
# ```
# # Expression tag: begins with '<%', ends with '%>'.
# # This expression does not need the local binding.
# ERB.new('Today is <%= Date::DAYNAMES[Date.today.wday] %>.').result # => "Today is Monday."
# # This expression tag does need the local binding.
# magic_word = 'xyzzy'
# template.result(binding) # => "The magic word is xyzzy."
# Here's how \ERB works:
#
# Execution tag: begins with '<%=', ends with '%>'.
# s = '<% File.write("t.txt", "Some stuff.") %>'
# ERB.new(s).result
# File.read('t.txt') # => "Some stuff."
# - You can create an \ERB object (a *template*) to store text that includes specially formatted *tags*.
# - You can call instance method ERB#result to get the *result*.
#
# # Comment tag: begins with '<%#', ends with '%>'.
# s = 'Some stuff;<%# Note to self: figure out what the stuff is. %> more stuff.'
# ERB.new(s).result # => "Some stuff; more stuff."
# ```
# \ERB supports tags of three kinds:
#
# - [Expression tags][expression tags]:
# each begins with `'<%'`, ends with `'%>'`; contains a Ruby expression;
# in the result, the value of the expression replaces the entire tag:
#
# magic_word = 'xyzzy'
# template.result(binding) # => "The magic word is xyzzy."
#
# ERB.new('Today is <%= Date::DAYNAMES[Date.today.wday] %>.').result # => "Today is Monday."
#
# The first call to #result passes argument `binding`,
# which contains the binding of variable `magic_word` to its string value `'xyzzy'`.
#
# The second call need not pass a binding,
# because its expression `Date::DAYNAMES` is globally defined.
#
# - [Execution tags][execution tags]:
# each begins with `'<%='`, ends with `'%>'`; contains Ruby code to be executed:
#
# s = '<% File.write("t.txt", "Some stuff.") %>'
# ERB.new(s).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.
#
# s = 'Some stuff;<%# Note to self: figure out what the stuff is. %> more stuff.'
# ERB.new(s).result # => "Some stuff; more stuff."
#
# ## Some Simple Examples
#
Expand Down Expand Up @@ -361,7 +371,7 @@
# ```
#
# You can give `trim_mode: '<>'` to suppress the trailing newline
# for each line that both begins with `'<%'` and ends with `'%<'`:
# for each line that both begins with `'<%'` and ends with `'%>'`:
#
# ```
# ERB.new(s, trim_mode: '<>').result.lines.each {|line| puts line.inspect }
Expand All @@ -373,7 +383,7 @@
#
# You can combine certain trim modes:
#
# - `'%-'`: Enable shorthand and omit each blank line ending with `'%>'`.
# - `'%-'`: Enable shorthand and omit each blank line ending with `'-%>'`.
# - `'%>'`: Enable shorthand and omit newline for each line ending with `'%>'`.
# - `'%<>'`: Enable shorthand and omit newline for each line starting with `'<%'` and ending with `'%>'`.
#
Expand Down Expand Up @@ -615,7 +625,17 @@ class ERB
Revision = '$Date:: $' # :nodoc: #'
deprecate_constant :Revision

# Returns revision information for the erb.rb module.
# :markup: markdown
#
# :call-seq:
# self.version -> string
#
# Returns the string revision for \ERB:
#
# ```
# ERB.version # => "4.0.4"
# ```
#
def self.version
VERSION
end
Expand Down Expand Up @@ -762,12 +782,30 @@ def run(b=new_toplevel)
print self.result(b)
end

# :markup: markdown
#
# :call-seq:
# result(binding = top_level) -> string
#
# Formats the string stored in template `self`;
# returns the string result:
#
# - Each [expression tag][expression tags] is replaced by the value of the embedded expression.
# - Each [execution tag][execution tags] is removed, and its embedded code is executed.
# - Each [comment tag][comment tags] is removed.
#
# See examples at the links.
#
# Executes the generated ERB code to produce a completed template, returning
# the results of that code.
# Argument `binding` is a [binding object],
# which should contain the bindings needed for all expression tags;
# the default is #top_level.
# See [Bindings][bindings].
#
# _b_ accepts a Binding object which is used to set the context of
# code evaluation.
# [binding object]: https://docs.ruby-lang.org/en/master/Binding.html
# [bindings]: rdoc-ref:ERB@Bindings
# [comment tags]: rdoc-ref:ERB@Comment+Tags
# [execution tags]: rdoc-ref:ERB@Execution+Tags
# [expression tags]: rdoc-ref:ERB@Expression+Tags
#
def result(b=new_toplevel)
unless @_init.equal?(self.class.singleton_class)
Expand Down
13 changes: 10 additions & 3 deletions win32/Makefile.sub
Original file line number Diff line number Diff line change
Expand Up @@ -506,23 +506,30 @@ VPATH = $(arch_hdrdir)/ruby;$(hdrdir)/ruby;$(srcdir);$(srcdir)/missing;$(win_src
!ifndef GIT
GIT = git
!endif
GIT_TO_CHECK_C_OPTION = -C . --version >nul 2>&1
!if "$(HAVE_GIT)" == "yes" || "$(HAVE_GIT)" == "no"
!else if "$(GIT)" == ""
HAVE_GIT = no
!else if [for %I in ($(GIT)) do @if not "%~xI" == "" exit 1]
! if [%I $(GIT_TO_CHECK_C_OPTION)] == 0
! if [for %I in ($(GIT)) do @if not "%~$$PATH:I" == "" exit 1]
HAVE_GIT = yes
! else
HAVE_GIT = no
! endif
!else
! if [for %x in (%PATHEXT:;= %) do @($(GIT)%x $(GIT_TO_CHECK_C_OPTION) && exit 0)] == 0
! if [for %x in (%PATHEXT:;= %) do @for %I in ($(GIT)%x) do @if not "%~$$PATH:I" == "" exit 1]
HAVE_GIT = yes
! else
HAVE_GIT = no
! endif
!endif
!if "$(HAVE_GIT)" == "no"
!else if [for /f "tokens=3" %I in ('git --version') do @(\
for /f "delims=. tokens=1-2" %I in ("%I") do @(\
if %I lss 2 (exit 1) else if %J lss 10 (exit 1) else (exit 0)\
)\
)]
HAVE_GIT = no
!endif

!if defined(VCS)
!else if exist($(srcdir)/.git)
Expand Down