Permalink
Browse files

Different behavior of binding for 1.8 and 1.9

Ruby 1.8.x uses the receiver of #binding as self in the binding.

  Kernel.eval("self")         => Kernel
  Kernel.binding.eval("self") => Kernel
  eval("self")                => main
  binding.eval("self")        => main

Ruby 1.9.x uses the current call context as self in the binding.

  Kernel.eval("self")         => Kernel
  Kernel.binding.eval("self") => main # difference
  eval("self")                => main
  binding.eval("self")        => main

In case when we execute binding where caller is Kernel, self of binding
is 'Kernel' (this is valid for ruby 1.8). But for 1.9 self of binding
when caller is Kernel should be self from current context.

  Kernel.binding.self == self   # valid for ruby 1.9
  Kernel.binding.self == Kernel # valid for ruby 1.8

When you call #eval method, binding is created dynamically and self of
binding is in this case Kernel.
  • Loading branch information...
LTe committed May 31, 2013
1 parent ede2fd3 commit 5bce0b47364e3d1d1f1bd48a3024e8d9ea577660
View
@@ -26,7 +26,7 @@ def from_proc?
def self.setup(variables, code, constant_scope, recv=nil, location=nil)
bind = allocate()
bind.self = recv || variables.self
bind.self = self_context(recv, variables)
bind.variables = variables
bind.compiled_code = code
bind.constant_scope = constant_scope
@@ -0,0 +1,8 @@
# -*- encoding: us-ascii -*-
class Binding
def self.self_context(recv, variables)
recv || variables.self
end
private :self_context
end
@@ -0,0 +1,8 @@
# -*- encoding: us-ascii -*-
class Binding
def self.self_context(recv, variables)
recv.equal?(Kernel) ? recv : variables.self
end
private :self_context
end
@@ -6,6 +6,7 @@ autoload.rbc
module.rbc
module18.rbc
binding.rbc
binding18.rbc
proc.rbc
proc18.rbc
enumerable.rbc
@@ -7,6 +7,7 @@ autoload.rbc
module.rbc
module19.rbc
binding.rbc
binding19.rbc
proc.rbc
proc19.rbc
enumerable19.rbc
@@ -7,6 +7,7 @@ autoload.rbc
module.rbc
module19.rbc
binding.rbc
binding19.rbc
proc.rbc
proc19.rbc
enumerable19.rbc

0 comments on commit 5bce0b4

Please sign in to comment.