NUMBERS = [10, 20, 30, 40, 50]
def hash_amount
return ObjectSpace.count_objects[:T_HASH]
end
def measure_hash_amount
initially = hash_amount
yield
return hash_amount - initially
end
merged = measure_hash_amount do
NUMBERS.inject({}) do |target, current|
target.merge(current => 'hello')
end
end
returned = measure_hash_amount do
NUMBERS.inject({}) do |target, current|
target[current] = 'hello'
target
end
end
puts "Hashes created using #merge: #{merged}"
puts "Hashes created using a return: #{returned}"
TlVNQkVSUyA9IFsxMCwgMjAsIDMwLCA0MCwgNTBdCgpkZWYgaGFzaF9hbW91bnQKICByZXR1cm4gT2JqZWN0U3BhY2UuY291bnRfb2JqZWN0c1s6VF9IQVNIXQplbmQKCmRlZiBtZWFzdXJlX2hhc2hfYW1vdW50CiAgaW5pdGlhbGx5ID0gaGFzaF9hbW91bnQKCiAgeWllbGQKCiAgcmV0dXJuIGhhc2hfYW1vdW50IC0gaW5pdGlhbGx5CmVuZAoKbWVyZ2VkID0gbWVhc3VyZV9oYXNoX2Ftb3VudCBkbwogIE5VTUJFUlMuaW5qZWN0KHt9KSBkbyB8dGFyZ2V0LCBjdXJyZW50fAogICAgdGFyZ2V0Lm1lcmdlKGN1cnJlbnQgPT4gJ2hlbGxvJykKICBlbmQKZW5kCgpyZXR1cm5lZCA9IG1lYXN1cmVfaGFzaF9hbW91bnQgZG8KICBOVU1CRVJTLmluamVjdCh7fSkgZG8gfHRhcmdldCwgY3VycmVudHwKICAgIHRhcmdldFtjdXJyZW50XSA9ICdoZWxsbycKICAgIHRhcmdldAogIGVuZAplbmQKCnB1dHMgIkhhc2hlcyBjcmVhdGVkIHVzaW5nICNtZXJnZTogI3ttZXJnZWR9IgpwdXRzICJIYXNoZXMgY3JlYXRlZCB1c2luZyBhIHJldHVybjogI3tyZXR1cm5lZH0i