Project

General

Profile

Feature #6012

Proc#source_location also return the column

Added by rogerdpack (Roger Pack) almost 8 years ago. Updated 9 months ago.

Status:
Assigned
Priority:
Normal
Target version:
-
[ruby-core:42579]

Description

As originally suggested in http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/42418

Suggestion/feature request:
have #source_location also return the beginning column where it was defined.
["test.rb", 8, 33]

Thanks!
-roger-


Related issues

Related to CommonRuby - Feature #8751: Add offsets to method#source_locationOpen08/08/2013Actions

History

Updated by rogerdpack (Roger Pack) almost 8 years ago

oops make that a feature request, but I'm unable to edit them myself.
Cheers!
-r

#2

Updated by nahi (Hiroshi Nakamura) almost 8 years ago

  • Tracker changed from Bug to Feature

Updated by ko1 (Koichi Sasada) over 7 years ago

  • Category set to core
  • Assignee set to nobu (Nobuyoshi Nakada)
  • Target version set to 2.0.0

Updated by trans (Thomas Sawyer) over 7 years ago

Would this effect Method#source_location too?

I'm not sure I am really digging this idea. First of all it means I have to go back and fix some code. Secondly it means I have to always worry about the additional piece of data even though most of the time it doesn't matter. And if the return can vary between 2 or 3 elements that's another thing to worry with.

On the other hand I can understand that it could be useful information in some cases.

In times like this that I think "Embrace the Object".

proc.source_location #=> #<SourceLocation @file="foo.rb" @line=12 @column=14>

And then a few different methods could provide that information in various useful forms.

proc.source_location.to_a #=> ["foo.rb", 12, 14]
proc.source_location.to_s #=> "foo.rb:12"
proc.source_location.values_at(:file, :line) #=> ["foo.rb", 12]

Or what have you.

Updated by trans (Thomas Sawyer) over 7 years ago

BTW & OT: When is any one going to explain how we format code examples as monospace text on this site?

Updated by drbrain (Eric Hodel) over 7 years ago

On Feb 26, 2012, at 6:33 AM, Thomas Sawyer wrote:

BTW & OT: When is any one going to explain how we format code examples as monospace text on this site?

Click the RD button and use RD formatting (two spaces).

Here's a bash alias to help, which works for rdoc too.

alias rdindent='pr -l1 -o2'

Updated by trans (Thomas Sawyer) over 7 years ago

Thanks Eric! I ((never)) noticed that ((%RD%)) "button" before (hardly looks like a button).

Why did it put:

=begin
=end

In the textarea when I clicked on it? ... maybe I'll find out by submitting this...

=begin
What's with the =begin =end?

Testing 1 2 3...

Try ((em)) (({code})) ((|ls|)) ((%var%)).
=end

Sorry for the noise.

Updated by trans (Thomas Sawyer) over 7 years ago

Well, that failed miserably. LOL :-)

#9

Updated by shyouhei (Shyouhei Urabe) over 7 years ago

  • Status changed from Open to Assigned

Updated by yhara (Yutaka HARA) about 7 years ago

  • Target version changed from 2.0.0 to 2.6
#11

Updated by naruse (Yui NARUSE) almost 2 years ago

  • Target version deleted (2.6)

Updated by mame (Yusuke Endoh) 11 months ago

Now the abstract syntax tree has column information, so we can implement this issue. We even add the last point of method.

# test.rb
◆def foo # ◆: line 2, column 0
end★     # ★: line 3, column 3

p method(:foo).source_location #=> ["test.rb", 2, 0, 3, 3]

Updated by ioquatix (Samuel Williams) 10 months ago

If changing this API is too complicated due to backwards compatibility, why not introduce new more general API:

Method#source -> Source.new(path, line_number, line_count, code, ...)

Usage:

method.source.code
method.source.path
method.source.location -> [2, 0, 3, 3]

Maybe including byte offset and length would also be useful (for seek).

Updated by ioquatix (Samuel Williams) 9 months ago

I also wish there was some meaningful implementation of proc.source.hash that was reasonably consistent across invocations of Ruby. Even if it was just best effort.

Updated by ioquatix (Samuel Williams) 9 months ago

I was playing around with this idea trying to make an implementation of class Source.

Is the source file cached in Ruby? Or should we use File.read to load it into memory?

It seems inefficient for large files, to find line/column offset. It would be nice to have absolute offset to seek to.

Maybe it's possible for source_location to append one more thing - the actual source code - if possible. This would be useful for situations like eval, where you might define something for a path that doesn't actually exist, but the source code is still available.

Updated by duerst (Martin Dürst) 9 months ago

ioquatix (Samuel Williams) wrote:

I also wish there was some meaningful implementation of proc.source.hash that was reasonably consistent across invocations of Ruby. Even if it was just best effort.

Please make that a separate feature if you are serious about it.

Also available in: Atom PDF