I have found the line_number passed to class_eval, module_eval and instance_eval doesn't match the line numbers reported by the error. This behaviour is not explained by the ruby-doc which says: (use instance_eval as an example)
the optional second and third parameters supply a filename and starting line number that are used when reporting compilation errors.
All these three methods class_eval, module_eval and instance_eval accepts two additional params to specify the filename and lineno parameters set the text for error messages.
this question has a practical demo for this behavior.
However, I have found the calculation of line numbers more complex than the explanation. Here is a demo
class Thing
def add_method
a = %{
non_exist
}
instance_eval(a, 'dummy', 12)
end
end
# error will raise to 15 instead of 12 which is specified
puts Thing.new.add_method
The code above proves that the line_no param passed to instance_eval is not the line numbers reported by the error but is only related to the line_no.
I am wondering about the exact behavior of this params?