Skip to content

Instantly share code, notes, and snippets.

@craigw
Forked from james/gist:175877
Created August 26, 2009 22:31
Show Gist options
  • Save craigw/175901 to your computer and use it in GitHub Desktop.
Save craigw/175901 to your computer and use it in GitHub Desktop.
# Ruby tends to pass by reference.
# http://groups.google.com/group/ruby-talk-google/msg/aa5cdea350f46314
#
# Notice all the same object ID in the output.
#
class Test
def self.test
hash = {:entry => ""}
array = []
puts "Hash: #{hash.object_id}"
3.times {
array << self.test2(hash)
puts "Hash: #{hash.object_id}"
}
array
end
def self.test2(hash)
puts "Hash: #{hash.object_id}"
hash[:entry] += "Test"
hash
end
end
p Test.test
# Hash: 58570
# Hash: 58570
# Hash: 58570
# Hash: 58570
# Hash: 58570
# Hash: 58570
# Hash: 58570
# [{:entry=>"TestTestTest"}, {:entry=>"TestTestTest"}, {:entry=>"TestTestTest"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment