I don't understand your use case; I suspect you're making things more complicated than they need to be.
Is there a way to test if two strings are the same in memory?
Object#object_id
is what you're looking for.
Is there a way to modify in place all the strings of a and b, without duplicates?
You could keep a set of not all the object_id
s, similar to what you were already trying. You can retrieve the string with ObjectSpace#_id2ref
. Something like this:
require 'set'
set = Set.new
a = ["apple", "cherry"]
b = a.dup
b[1] = "blueberry"
# Collect unique string objects
a.each{|s| set << s.object_id}
b.each{|s| set << s.object_id}
# Make pie with each unique string object
set.each{|id| ObjectSpace._id2ref(id) << " pie"}
a
# => ["apple pie", "cherry pie"]
b
# => ["apple pie", "blueberry pie"]
That seems a bit crazy to me, though. Again, I think there's probably a better way to do what you're trying to do, but it's hard to tell based on the information provided.