Feature #6012
Proc#source_location also return the column
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
History
Updated by rogerdpack (Roger Pack) about 7 years ago
oops make that a feature request, but I'm unable to edit them myself.
Cheers!
-r
Updated by ko1 (Koichi Sasada) almost 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) almost 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) almost 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) almost 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) almost 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) almost 7 years ago
Well, that failed miserably. LOL :-)
Updated by yhara (Yutaka HARA) over 6 years ago
- Target version changed from 2.0.0 to 2.6
Updated by mame (Yusuke Endoh) about 2 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) 29 days 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) 7 days 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) 7 days 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) 7 days 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.