nobu.nokada / softhome.net wrote:
> Hi,
> 
> At Tue, 25 May 2004 05:39:16 +0900,
> Joel VanderWerf wrote in [ruby-talk:101253]:
> 
>>It would be easier to extract the entire code block without doing 
>>parsing if Proc#inspect returned a _range_ of lines:
>>
>>#<Proc:0x401c6d54 / file.rb:3..5>
> 
> 
> Unfortunately, there's no more room for it.

Oh, well. :/

>>I thought that eval-ing __LINE__ against the proc's binding might give 
>>the last line of the proc's definition, but actually the results are 
>>surprisingly inconsistent:
> 
> 
> I think this has no bad side effects.
> 
> 
> Index: eval.c > =================================================================== > RCS file: /cvs/ruby/src/ruby/eval.c,v > retrieving revision 1.665 > diff -u -2 -p -r1.665 eval.c > --- eval.c 25 May 2004 02:54:21 -0000 1.665 > +++ eval.c 25 May 2004 08:49:56 -0000 > @@ -2932,4 +2932,5 @@ rb_eval(self, n) > PUSH_ITER(ITER_PRE); > if (nd_type(node) == NODE_ITER) { > + ruby_frame->node = node; > result = rb_eval(self, node->nd_iter); > } > @@ -6043,8 +6044,8 @@ eval(self, src, scope, file, line) > old_wrapper = ruby_wrapper; > ruby_wrapper = data->wrapper; > - if ((file == 0 || (line == 1 && strcmp(file, "(eval)") == 0)) && data->body) { > - file = data->body->nd_file; > + if (file == 0 || (line == 1 && strcmp(file, "(eval)") == 0)) { > + file = data->frame.node->nd_file; > if (!file) file = "__builtin__"; > - line = nd_line(data->body); > + line = nd_line(data->frame.node); > } > >
> The effect of this is to make __LINE__ more consistent, but it's still the first line of the proc, just as is reported in Proc#inspect: $ cat t1.rb pr = proc { 1+1 puts } p eval("__LINE__", pr) p pr [~/ruby/prj/redshift/examples] ruby t1.rb 1 #<Proc:0x401c6e80 / t1.rb:1> I'm not really sure what __LINE__ should eval to, but this behavior doesn't help me find the start and end lines in the source file. I guess I can do some parsing, following Florian's example.