|
531 | 531 | # # => #<Encoding:Big5> |
532 | 532 | # ``` |
533 | 533 | # |
| 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 | +# |
534 | 577 | # ## Plain Text Example |
535 | 578 | # |
536 | 579 | # Here's a plain-text string; |
@@ -833,29 +876,32 @@ def make_compiler(trim_mode) |
833 | 876 | # The encoding to eval |
834 | 877 | attr_reader :encoding |
835 | 878 |
|
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 |
838 | 885 | attr_accessor :filename |
839 | 886 |
|
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 |
842 | 893 | attr_accessor :lineno |
843 | 894 |
|
| 895 | + # :markup: markdown |
844 | 896 | # |
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 |
852 | 900 | # |
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]. |
858 | 903 | # |
| 904 | + # [error reporting]: rdoc-ref:ERB@Error+Reporting |
859 | 905 | def location=((filename, lineno)) |
860 | 906 | @filename = filename |
861 | 907 | @lineno = lineno if lineno |
|
0 commit comments