Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Binding#local_variables and Binding#receiver
Browse files Browse the repository at this point in the history
ruipserra authored and Yorick Peterse committed Mar 11, 2015
1 parent ef0d44d commit 59d6f28
Showing 5 changed files with 40 additions and 28 deletions.
14 changes: 13 additions & 1 deletion kernel/common/binding.rb
Original file line number Diff line number Diff line change
@@ -3,8 +3,16 @@ class Binding
attr_accessor :compiled_code
attr_accessor :constant_scope
attr_accessor :proc_environment
attr_accessor :self
attr_accessor :location
attr_reader :receiver

def self=(obj)
@receiver = obj
end

def self
@receiver
end

def from_proc?
@proc_environment
@@ -54,4 +62,8 @@ def eval(expr, filename=nil, lineno=nil)

Kernel.eval(expr, self, filename, lineno)
end

def local_variables
variables.local_variables
end
end
22 changes: 1 addition & 21 deletions kernel/common/eval.rb
Original file line number Diff line number Diff line change
@@ -107,28 +107,8 @@ module Kernel
# Names of local variables at point of call (including evaled)
#
def local_variables
locals = []

scope = Rubinius::VariableScope.of_sender

# Ascend up through all applicable blocks to get all vars.
while scope
if scope.method.local_names
scope.method.local_names.each do |name|
locals << name
end
end

if dyn = scope.dynamic_locals
dyn.keys.each do |name|
locals << name unless locals.include?(name)
end
end

scope = scope.parent
end

locals
scope.local_variables
end
module_function :local_variables

26 changes: 26 additions & 0 deletions kernel/common/variable_scope.rb
Original file line number Diff line number Diff line change
@@ -101,6 +101,32 @@ def local_layout
out
end

# Returns the names of local variables available in this scope
#
def local_variables
locals = []
scope = self

# Ascend up through all applicable blocks to get all vars.
while scope
if scope.method.local_names
scope.method.local_names.each do |name|
locals << name
end
end

if dyn = scope.dynamic_locals
dyn.keys.each do |name|
locals << name unless locals.include?(name)
end
end

scope = scope.parent
end

locals
end

def exitted?
@exitted
end
5 changes: 0 additions & 5 deletions spec/tags/ruby/core/binding/local_variables_tags.txt

This file was deleted.

1 change: 0 additions & 1 deletion spec/tags/ruby/core/binding/receiver_tags.txt

This file was deleted.

0 comments on commit 59d6f28

Please sign in to comment.