fork download
  1. NUMBERS = [10, 20, 30, 40, 50]
  2.  
  3. def hash_amount
  4. return ObjectSpace.count_objects[:T_HASH]
  5. end
  6.  
  7. def measure_hash_amount
  8. initially = hash_amount
  9.  
  10. yield
  11.  
  12. return hash_amount - initially
  13. end
  14.  
  15. merged = measure_hash_amount do
  16. NUMBERS.inject({}) do |target, current|
  17. target.merge(current => 'hello')
  18. end
  19. end
  20.  
  21. returned = measure_hash_amount do
  22. NUMBERS.inject({}) do |target, current|
  23. target[current] = 'hello'
  24. target
  25. end
  26. end
  27.  
  28. puts "Hashes created using #merge: #{merged}"
  29. puts "Hashes created using a return: #{returned}"
Success #stdin #stdout 0.01s 7428KB
stdin
Standard input is empty
stdout
Hashes created using #merge: 12
Hashes created using a return: 2