Skip to content

Instantly share code, notes, and snippets.

@kschiess
Forked from floere/embedded_strings.rb
Created August 23, 2013 07:31
Show Gist options
  • Save kschiess/6316508 to your computer and use it in GitHub Desktop.
Save kschiess/6316508 to your computer and use it in GitHub Desktop.
$:.unshift 'PATHTOINTERNALSCODE'
require 'string_internals.bundle'
def scan_for title, s, indent=0
GC.start
times = 0
content = ''
ObjectSpace.each_object(String) do |str|
if str == s
times += 1
content << " " * indent
content << " %d " % str.object_id
content << str.internals.
select { |k,v| v }.
map { |k,v| v==true ? k : "#{k}=#{v.object_id}" }.
join(', ')
content << "\n"
end
end
puts " "*indent + "string(#{s[0,10].inspect}) occurs #{times} times: {"
puts content
puts " "*indent + "}"
end
def print_parts parts
puts "Parts array: {"
parts.each_with_index do |p, i|
scan_for "part #{i}", p, 2
end
puts "}"
end
def experiment s
parts = 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?
scan_for 'original', s
print_parts parts
end
s = "One Two Three" # Shorter than 24 characters.
experiment s
puts
s = "One Two Three Four Five" # Shorter than 24 characters.
experiment s
puts
s = "One Two Three Four Five Six" # Longer than 24 characters.
experiment s
puts
s = "a" * 30 + " " + "b" * 30
experiment s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment