Implement code_range in Proc and Method
Add a new method
code_range as an alternative to
source_location to Proc and Method
I'd like to get a body from a Proc in TraceLocation gem (https://github.com/yhirano55/trace_location), in order to add what's executed to the output. There's no way to do that in current Ruby implementation, so as an alternative, I considered getting source code location of a Proc.
I propose that
Method#code_range. Other names can work as well, for example
Proc#source_region. It returns an array containing filename as a first argument and position information as a second array. For example:
a_proc.position # => [(irb), [1, 5, 3, 25]]
I've implemented a simpler version of this, see gist for more details.
Notice I use
code_location from iseq struct.
One might say that we can simply add columns and end position to Proc#source_location. However, this can easily brake existing apps such as Pry.
It's also possible that we add additional keyword argument to
Proc#source_location, for instance:
This change can also break existing apps since in old Rubies this keyword argument cannot be accepted.
Therefore, adding a new method is better in terms of backward compatibility. It might be better at readability as well.
I propose an API to get code position of Proc and Method so that we can get body of them (especially of a Proc).