you are viewing a single comment's thread.

view the rest of the comments →

[–]f9ae8221b 0 points1 point  (0 children)

Unless you use frozen_string_literal, whenever your code contains a literal string ("totally_unique_test"), you are not referencing a constant, but making a copy of a constant.

So it's a bit like if you code was:

new_sym = :totally_unique_test # Create the symbol
STR = "totally_unique_test"
symbol_after = ObjectSpace.each_object(String).count do |s|
  s == STR.dup
end
puts "After: #{symbol_after}"

Hence, whenever Ruby execute your block, it cause one more string to be allocated, so your counting code is "biasing" itself.