ruby - How does module_eval / class_eval / instance_eval counts the line numbers -
i have found line_number passed class_eval
, module_eval
, instance_eval
doesn't match line numbers reported error. behaviour not explained ruby-doc says: (use instance_eval
example)
the optional second , third parameters supply filename , starting line number used when reporting compilation errors.
all these 3 methods class_eval
, module_eval
, instance_eval
accepts 2 additional params specify filename , lineno parameters set text error messages.
this question has practical demo behavior.
however, have found calculation of line numbers more complex explanation. here demo
class thing def add_method = %{ non_exist } instance_eval(a, 'dummy', 12) end end # error raise 15 instead of 12 specified puts thing.new.add_method
the code above proves line_no
param passed instance_eval
not line numbers reported error related line_no
.
i wondering exact behavior of params?
as snippet of documentation states, lineno
specifies starting line number of pseudo-file. string eval contains 3 lines, second line contains non_exist
(%{}
preserves line breaks).
when execute code, error in line 14, not 15 receive. have expected 13, seems ruby parser "notice" error on next line, possibly looking method arguments or else make sense of non_exists
(i not sure on that). if insert blank line (that contains no indentation) after non_exists
, expected 13.
any lines inserted after %{
before non_exists
increment line number in error one, should expected.
Comments
Post a Comment