ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
From: eregontp@gmail•com
To: ruby-dev@ruby-lang.org
Subject: [ruby-dev:50850] [Ruby master Feature#16142] Implement code_range in Proc and Method
Date: Fri, 18 Oct 2019 18:08:57 +0000 (UTC)	[thread overview]
Message-ID: <redmine.journal-82177.20191018180856.c98e99c2539eeb9b@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-16142.20190904054650@ruby-lang.org

Issue #16142 has been updated by Eregon (Benoit Daloze).


Just a note: "code range" is an implementation-level concept for Strings. For instance there is CR_7BIT which means all characters are < 128.
So I would recommend another method name than `code_range`, as I think that will be confusing.

IMHO the name should be very similar to `source_location` since it is highly related.

I think we should just extend `source_location` and fix the few existing usages that might be problematic.
Detecting whether the method takes keywords should be easy either by trying and rescuing ArgumentError, or using UnboundMethod#arity.

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

* Author: okuramasafumi (Masafumi OKURA)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
# 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/

  parent reply	other threads:[~2019-10-18 18:09 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <redmine.issue-16142.20190904054650@ruby-lang.org>
2019-09-04  5:46 ` [ruby-dev:50845] [Ruby master Feature#16142] Implement code_range in Proc and Method masafumi.o1988
2019-10-17  2:36 ` [ruby-dev:50848] " manga.osyo
2019-10-18 18:08 ` eregontp [this message]
2019-11-12  3:42 ` [ruby-dev:50860] " samuel
2019-11-27 17:42 ` [ruby-dev:50868] " eregontp
2019-11-28  4:46 ` [ruby-dev:50869] " samuel
2019-11-28  7:43 ` [ruby-dev:50870] " matz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.ruby-lang.org/en/community/mailing-lists/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=redmine.journal-82177.20191018180856.c98e99c2539eeb9b@ruby-lang.org \
    --to=ruby-dev@ruby-lang.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).