Created
May 15, 2014 09:51
-
-
Save deepak/84d70b1ac6bcc64dd820 to your computer and use it in GitHub Desktop.
local variable shadows ivar
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
class Foo | |
attr_accessor :params | |
def initialize(params) | |
@params = params | |
end | |
def process(params) | |
puts "====> before: #{params.inspect}" | |
params.merge!(local: "modifications") | |
puts "====> after: #{params.inspect}" | |
end | |
end | |
params = { initial: "hash" } | |
foo = Foo.new(params) | |
puts "====> initial global: #{params.inspect}" | |
puts "====> initial in foo: #{foo.params.inspect}" | |
foo.process(something: "new") | |
puts "====> foo: #{foo.params.inspect}" | |
__END__ | |
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.3.0] | |
====> initial global: {:initial=>"hash"} | |
====> initial in foo: {:initial=>"hash"} | |
====> before: {:something=>"new"} | |
====> after: {:something=>"new", :local=>"modifications"} | |
====> foo: {:initial=>"hash"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usecase is not about shadowing, but about modifying what is passed.