Skip to content

Commit 3fa1eb7

Browse files
BurdetteLamarmatzbot
authored andcommitted
[ruby/erb] [DOC] Adds section 'Error Reporting'
(ruby/erb#75) ruby/erb@8dc0eacaad
1 parent 3c7a897 commit 3fa1eb7

File tree

1 file changed

+62
-16
lines changed

1 file changed

+62
-16
lines changed

lib/erb.rb

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,49 @@
531531
# # => #<Encoding:Big5>
532532
# ```
533533
#
534+
# ## Error Reporting
535+
#
536+
# Consider this template (containing an error):
537+
#
538+
# ```
539+
# s = '<%= nosuch %>'
540+
# template = ERB.new(s)
541+
# ```
542+
#
543+
# When \ERB reports an error,
544+
# it includes a file name (if available) and a line number;
545+
# the file name comes from method #filename, the line number from method #lineno.
546+
#
547+
# Initially, those values are `nil` and `0`, respectively;
548+
# these initial values are reported as `'(erb)'` and `1`, respectively:
549+
#
550+
# ```
551+
# template.filename # => nil
552+
# template.lineno # => 0
553+
# template.result
554+
# (erb):1:in '<main>': undefined local variable or method 'nosuch' for main (NameError)
555+
# ```
556+
#
557+
# You can use methods #filename= and #lineno= to assign values
558+
# that are more meaningful in your context:
559+
#
560+
# ```
561+
# template.filename = 't.txt'
562+
# # => "t.txt"
563+
# template.lineno = 555
564+
# # => 555
565+
# template.result
566+
# t.txt:556:in '<main>': undefined local variable or method 'nosuch' for main (NameError)
567+
# ```
568+
#
569+
# You can use method #location= to set both values:
570+
#
571+
# ```
572+
# template.location = ['u.txt', 999]
573+
# template.result
574+
# u.txt:1000:in '<main>': undefined local variable or method 'nosuch' for main (NameError)
575+
# ```
576+
#
534577
# ## Plain Text Example
535578
#
536579
# Here's a plain-text string;
@@ -833,29 +876,32 @@ def make_compiler(trim_mode)
833876
# The encoding to eval
834877
attr_reader :encoding
835878

836-
# The optional _filename_ argument passed to Kernel#eval when the ERB code
837-
# is run
879+
# :markup: markdown
880+
#
881+
# Sets or returns the file name to be used in reporting errors;
882+
# see [Error Reporting][error reporting].
883+
#
884+
# [error reporting]: rdoc-ref:ERB@Error+Reporting
838885
attr_accessor :filename
839886

840-
# The optional _lineno_ argument passed to Kernel#eval when the ERB code
841-
# is run
887+
# :markup: markdown
888+
#
889+
# Sets or returns the line number to be used in reporting errors;
890+
# see [Error Reporting][error reporting].
891+
#
892+
# [error reporting]: rdoc-ref:ERB@Error+Reporting
842893
attr_accessor :lineno
843894

895+
# :markup: markdown
844896
#
845-
# Sets optional filename and line number that will be used in ERB code
846-
# evaluation and error reporting. See also #filename= and #lineno=
847-
#
848-
# erb = ERB.new('<%= some_x %>')
849-
# erb.render
850-
# # undefined local variable or method `some_x'
851-
# # from (erb):1
897+
# :call-seq:
898+
# location = [filename, lineno] => [filename, lineno]
899+
# location = filename -> filename
852900
#
853-
# erb.location = ['file.erb', 3]
854-
# # All subsequent error reporting would use new location
855-
# erb.render
856-
# # undefined local variable or method `some_x'
857-
# # from file.erb:4
901+
# Sets the values of #filename and, if given, #lineno;
902+
# see [Error Reporting][error reporting].
858903
#
904+
# [error reporting]: rdoc-ref:ERB@Error+Reporting
859905
def location=((filename, lineno))
860906
@filename = filename
861907
@lineno = lineno if lineno

0 commit comments

Comments
 (0)