ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-dev:52064]  [Ruby master Feature#16142] Implement code_range in Proc and Method
       [not found] <redmine.issue-16142.20190904054650.23655@ruby-lang.org>
@ 2024-01-09 15:43 ` okuramasafumi (Masafumi OKURA) via ruby-dev
  2024-01-09 18:30 ` [ruby-dev:52065] " byroot (Jean Boussier) via ruby-dev
  1 sibling, 0 replies; 2+ messages in thread
From: okuramasafumi (Masafumi OKURA) via ruby-dev @ 2024-01-09 15:43 UTC (permalink / raw)
  To: ruby-dev; +Cc: okuramasafumi (Masafumi OKURA)

Issue #16142 has been updated by okuramasafumi (Masafumi OKURA).


Hi I noticed we didn't get any conclusion on this topic.
I agree that method name should be something different. It follows the Matz's question: "What do we need from the method? line numbers? offset? whatever?
"

https://docs.ruby-lang.org/en/master/IO.html#method-c-read
And we have `IO.read` method that accepts `length` and `offset` parameter. Since it's expected to be used with `IO.read`, it is convenient to return these two values.

And Matz says

> Making the return value as a specific class seems overkill.

And I agree. So the return value could be either Array or Hash. I think Hash is better than Array in this case because the value such as `[19, 10]` doesn't make sense itself.

So what would be a good name? The combination of length and offset doesn't have a specific name as far as I know. What about `location` if `Proc` has this method?
The final version would look like this.

```ruby
my_proc = proc do
  do_something
end
loc = my_proc.location
p loc # => {length:10, offset:19} maybe wrong number

p File.read(__FILE__, loc[:length], loc[:offset]) # => do_something
```

----------------------------------------
Feature #16142: Implement code_range in Proc and Method
https://bugs.ruby-lang.org/issues/16142#change-106122

* Author: okuramasafumi (Masafumi OKURA)
* Status: Open
* Priority: Normal
----------------------------------------
# Abstract

Add a new method `code_range` as an alternative to `source_location` to Proc and Method

# Background

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.

# Proposal

I propose that `Proc#code_range` and `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]]`

# Implementation

I've implemented a simpler version of this, see gist for more details.

https://gist.github.com/okuramasafumi/ac90bbf04a1c13b7d67954c9c5e62553

Notice I use `code_location` from iseq struct.

# Discussion

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:
`a_proc.source_location(including_range: true)`
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.

# Summary

I propose an API to get code position of Proc and Method so that we can get body of them (especially of a Proc).



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [ruby-dev:52065]  [Ruby master Feature#16142] Implement code_range in Proc and Method
       [not found] <redmine.issue-16142.20190904054650.23655@ruby-lang.org>
  2024-01-09 15:43 ` [ruby-dev:52064] [Ruby master Feature#16142] Implement code_range in Proc and Method okuramasafumi (Masafumi OKURA) via ruby-dev
@ 2024-01-09 18:30 ` byroot (Jean Boussier) via ruby-dev
  1 sibling, 0 replies; 2+ messages in thread
From: byroot (Jean Boussier) via ruby-dev @ 2024-01-09 18:30 UTC (permalink / raw)
  To: ruby-dev; +Cc: byroot (Jean Boussier)

Issue #16142 has been updated by byroot (Jean Boussier).


> So what would be a good name?

What about `position`? I think it goes well together with `location`.

----------------------------------------
Feature #16142: Implement code_range in Proc and Method
https://bugs.ruby-lang.org/issues/16142#change-106130

* Author: okuramasafumi (Masafumi OKURA)
* Status: Open
* Priority: Normal
----------------------------------------
# Abstract

Add a new method `code_range` as an alternative to `source_location` to Proc and Method

# Background

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.

# Proposal

I propose that `Proc#code_range` and `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]]`

# Implementation

I've implemented a simpler version of this, see gist for more details.

https://gist.github.com/okuramasafumi/ac90bbf04a1c13b7d67954c9c5e62553

Notice I use `code_location` from iseq struct.

# Discussion

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:
`a_proc.source_location(including_range: true)`
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.

# Summary

I propose an API to get code position of Proc and Method so that we can get body of them (especially of a Proc).



-- 
https://bugs.ruby-lang.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-01-09 18:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-16142.20190904054650.23655@ruby-lang.org>
2024-01-09 15:43 ` [ruby-dev:52064] [Ruby master Feature#16142] Implement code_range in Proc and Method okuramasafumi (Masafumi OKURA) via ruby-dev
2024-01-09 18:30 ` [ruby-dev:52065] " byroot (Jean Boussier) via ruby-dev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).