Skip to content

Instantly share code, notes, and snippets.

@floere
Last active December 21, 2015 13:49
Show Gist options
  • Save floere/6315088 to your computer and use it in GitHub Desktop.
Save floere/6315088 to your computer and use it in GitHub Desktop.
String#split behaviour with embedded strings.
s = "One Two Three" # Shorter than 24 characters.
s.split /\s/ # Uncomment and comment. Why does the original string now occur 2 more times (1 per match?) in the ObjectSpace than without splitting?
times = 0
ObjectSpace.each_object(String) do |str|
times += 1 if str == s
end
p times
s = "One Two Three Four Five" # Shorter than 24 characters.
s.split /\s/ # Uncomment and comment. Why does the original string now occur 4 more times (1 per match?) in the ObjectSpace than without splitting?
times = 0
ObjectSpace.each_object(String) do |str|
times += 1 if str == s && str.hash == s.hash
end
p times
s = "One Two Three Four Five Six" # Longer than 24 characters.
s.split /\s/ # Uncomment and comment.
times = 0
ObjectSpace.each_object(String) do |str|
times += 1 if str == s && str.hash == s.hash
end
p times
@floere
Copy link
Author

floere commented Aug 23, 2013

Running GC.start after the splits cleans up the garbage left behind by #split.

@kschiess
Copy link

... This modified version behaves deterministically:
https://gist.github.com/kschiess/6316508

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment