Binding#eval issue

I

Intransition

I have the following extensions to Binding:

class Binding

# Returns line number of the binding.
def __LINE__
eval("__LINE__")
end

# Returns file name of the binding.
def __FILE__
eval("__FILE__")
end

end

The work fine in 1.8.7 (or lower when you define Binding#eval), but
fails in 1.9 where it returns "(eval)" instead of the file name and
nothing at all for the line number.

Any idea on how to fix? Should this be considered a 1.9 bug?
 
B

Brian Candler

Thomas said:
I have the following extensions to Binding:

class Binding

# Returns line number of the binding.
def __LINE__
eval("__LINE__")
end

# Returns file name of the binding.
def __FILE__
eval("__FILE__")
end

end

The work fine in 1.8.7

I tried this, and was surprised to find that 1.8.7 makes a special case
of eval inside an instance method of Binding:

$ irb --simple-prompt=> "(irb)"

This is particularly surprising given that instance_eval doesn't show
this behaviour:
=> "(eval)"

__FILE__ is the filename where the source being executed is found. When
eval'ing from a string, the source is not in any particular file and so
I'd always expect "(eval)" to be returned, and __LINE__ to be relative
to the start of the string, unless you pass file and/or line numbers as
extra args to eval.

In ruby 1.9.2 this works as I'd expect:

$ irb19 --simple-prompt=> "(eval)"

So I'd say that 1.8.7 is the anomoly.
 
I

Intransition

I tried this, and was surprised to find that 1.8.7 makes a special case
of eval inside an instance method of Binding:

Right. I thought that was idea because you want to know about the
binding, not the instance of Binding.
$ irb --simple-prompt>> RUBY_VERSION
=3D> "1.8.7"

=3D> "(irb)"

This is particularly surprising given that instance_eval doesn't show
this behaviour:


=3D> "(eval)"

__FILE__ is the filename where the source being executed is found. When
eval'ing from a string, the source is not in any particular file and so
I'd always expect "(eval)" to be returned, and __LINE__ to be relative
to the start of the string, unless you pass file and/or line numbers as
extra args to eval.

In ruby 1.9.2 this works as I'd expect:

$ irb19 --simple-prompt>> RUBY_REVISION
=3D> 24186

=3D> "(eval)"

So I'd say that 1.8.7 is the anomoly.

In that case there would be no way to define these manually. And I
would need to request that Binding support __LINE__ and __FILE__ in
core.

But I'm not so sure, because there has to be a way to access the
binding itself or what is the point? Perhaps Binding needs a special
method, ie. #binding_eval, which would eval as if in the binding.
 
W

Walton Hoops

In that case there would be no way to define these manually. And I
would need to request that Binding support __LINE__ and __FILE__ in
core.

But I'm not so sure, because there has to be a way to access the
binding itself or what is the point? Perhaps Binding needs a special
method, ie. #binding_eval, which would eval as if in the binding.
You can just pass your binding to eval. See:
http://ruby-doc.org/core/classes/Kernel.html#M005922
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,144
Latest member
KetoBaseReviews
Top