Skip to content

Instantly share code, notes, and snippets.

@macks
Created May 8, 2011 06:37
Show Gist options
  • Save macks/961171 to your computer and use it in GitHub Desktop.
Save macks/961171 to your computer and use it in GitHub Desktop.
local variables in eval
require 'rubygems'
require 'rspec'
describe 'Local variable in eval' do
context 'Assignment in eval' do
it 'works' do
expect {
eval <<-EOS
var = true
raise unless var
EOS
}.not_to raise_error
end
end
context 'Assignment in eval in eval' do
it 'fails with NameError' do
expect {
eval <<-EOS
eval "var = true"
var
EOS
}.to raise_error(NameError)
end
it 'succeeds if assigned in advance' do
expect {
eval <<-EOS
var = false
eval "var = true"
raise unless var
EOS
}.not_to raise_error
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment