Last active
December 21, 2015 13:49
-
-
Save floere/6315088 to your computer and use it in GitHub Desktop.
String#split behaviour with embedded strings.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
... This modified version behaves deterministically:
https://gist.github.com/kschiess/6316508