-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added local variable methods to Binding
This adds the following methods: * Binding#local_variable_set * Binding#local_variable_get * Binding#local_variable_defined? Fixes #2992 Closes #3372
- Loading branch information
Yorick Peterse
committed
Feb 5, 2016
1 parent
391db8d
commit 40f04c1
Showing
5 changed files
with
195 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
require File.expand_path('../../../spec_helper', __FILE__) | ||
|
||
describe 'Binding#local_variable_defined?' do | ||
it 'returns false when a variable is not defined' do | ||
binding.local_variable_defined?(:foo).should == false | ||
end | ||
|
||
it 'returns true when a regular local variable is defined' do | ||
foo = 10 | ||
|
||
binding.local_variable_defined?(:foo).should == true | ||
end | ||
|
||
it 'returns true when a local variable is defined using eval()' do | ||
bind = binding | ||
|
||
bind.eval('foo = 10') | ||
|
||
bind.local_variable_defined?(:foo).should == true | ||
end | ||
|
||
it 'returns true when a local variable is defined using Binding#local_variable_set' do | ||
bind = binding | ||
|
||
bind.local_variable_set(:foo, 10) | ||
|
||
bind.local_variable_defined?(:foo).should == true | ||
end | ||
|
||
it 'returns true when a local variable is defined in a parent scope' do | ||
foo = 10 | ||
|
||
proc { binding.local_variable_defined?(:foo) }.call.should == true | ||
end | ||
|
||
it 'allows usage of a String as the variable name' do | ||
foo = 10 | ||
|
||
binding.local_variable_defined?('foo').should == true | ||
end | ||
|
||
it 'allows usage of an object responding to #to_str as the variable name' do | ||
foo = 10 | ||
name = mock(:obj) | ||
|
||
name.stub!(:to_str).and_return('foo') | ||
|
||
binding.local_variable_defined?(name).should == true | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require File.expand_path('../../../spec_helper', __FILE__) | ||
|
||
describe 'Binding#local_variable_get' do | ||
it 'gets a local variable defined before the Binding' do | ||
number = 10 | ||
|
||
binding.local_variable_get(:number).should == 10 | ||
end | ||
|
||
it 'gets a local variable defined in the Binding' do | ||
bind = binding | ||
|
||
bind.local_variable_set(:number, 10) | ||
bind.local_variable_get(:number).should == 10 | ||
end | ||
|
||
it 'gets a local variable defined using eval()' do | ||
bind = binding | ||
|
||
bind.eval('number = 10') | ||
|
||
bind.local_variable_get(:number).should == 10 | ||
end | ||
|
||
it 'gets a local variable defined in a parent scope' do | ||
number = 10 | ||
|
||
proc { binding.local_variable_get(:number) }.call.should == 10 | ||
end | ||
|
||
it 'raises NameError for an undefined local variable' do | ||
proc { binding.local_variable_get(:cats) }.should raise_error(NameError) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
require File.expand_path('../../../spec_helper', __FILE__) | ||
|
||
describe 'Binding#local_variable_set' do | ||
it 'sets a new local variable' do | ||
bind = binding | ||
|
||
bind.local_variable_set(:number, 10) | ||
bind.local_variable_get(:number).should == 10 | ||
end | ||
|
||
it 'sets a local variable using a String as the variable name' do | ||
bind = binding | ||
|
||
bind.local_variable_set('number', 10) | ||
bind.local_variable_get('number').should == 10 | ||
end | ||
|
||
it 'sets a local variable using an object responding to #to_str as the variable name' do | ||
bind = binding | ||
name = mock(:obj) | ||
|
||
name.stub!(:to_str).and_return('number') | ||
|
||
bind.local_variable_set(name, 10) | ||
bind.local_variable_get(name).should == 10 | ||
end | ||
|
||
it 'scopes new local variables to the receiving Binding' do | ||
bind = binding | ||
|
||
bind.local_variable_set(:number, 10) | ||
|
||
proc { number }.should raise_error(NameError) | ||
end | ||
|
||
it 'overwrites an existing local variable defined before a Binding' do | ||
number = 10 | ||
bind = binding | ||
|
||
bind.local_variable_set(:number, 20) | ||
|
||
number.should == 20 | ||
end | ||
|
||
it 'overwrites a local variable defined using eval()' do | ||
bind = binding | ||
|
||
bind.eval('number = 10') | ||
|
||
bind.local_variable_set(:number, 20) | ||
|
||
bind.local_variable_get(:number).should == 20 | ||
end | ||
end |