method_source icon indicating copy to clipboard operation
method_source copied to clipboard

Extracted source continues to next expression on same line

Open sawa opened this issue 10 years ago • 4 comments

Extracted source continues to the next expression on the same line

def foo; end; def bar
  "baz"
end

method(:foo).source.display

output:

def foo; end; def bar
  "baz"
end

I expected:

def foo; end; def bar

Is this bug or feature?

sawa avatar Feb 10 '15 16:02 sawa

It's because we use a simplistic approach to extracting the code, we read line by line looking for complete expressions. This works in 90% of cases as users don't typically define more than one method on a line.

banister avatar Feb 10 '15 17:02 banister

I guess it wouldn't be very hard to try advancing by semicolon-separated chunks and not just complete lines.

On Tue, Feb 10, 2015 at 9:06 AM, John Mair [email protected] wrote:

It's because we use a simplistic approach to extracting the code, we read line by line looking for complete expressions. This works in 90% of cases as users don't typically define more than one method on a line.

Reply to this email directly or view it on GitHub: https://github.com/banister/method_source/issues/28#issuecomment-73738898

rf- avatar Feb 10 '15 18:02 rf-

Since you are not aiming for dynamically defined methods, all method definitions will follow def ... end syntax. Therefore, you don't even have to go through line by line. Rather, you can go through each positions that ends with end, whether or not that is in the middle of a line. That would in general be more efficient since you can skip line, and at the same time be more accurate to handle cases like this.

sawa avatar Feb 11 '15 05:02 sawa

We actually do support dynamically-defined methods. It's also not uncommon for a source_location to point to something like an attr_accessor call.

rf- avatar Feb 11 '15 06:02 rf-