Extracted source continues to next expression on same line
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?
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.
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
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.
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.