gpt4 book ai didi

ruby - 为什么 2 个相同的字符串在 Ruby 中具有相同的 object_id?

转载 作者:数据小太阳 更新时间:2023-10-29 07:46:11 26 4
gpt4 key购买 nike

您可能知道,在 Ruby 中,两个相同的字符串没有相同的 object_id,而两个相同的符号却有。例如:

irb(main):001:0> :george.object_id == :george.object_id
=> true
irb(main):002:0> "george".object_id == "george".object_id
=> false

但是,在我下面的代码中,它显示具有相同值“one”的两个字符串具有相同的 object_id。

class MyArray < Array
def ==(x)
comparison = Array.new()
x.each_with_index{|item, i| comparison.push(item.object_id.equal?(self[i].object_id))}
if comparison.include?(false) then
false
else
true
end
end
end
class MyHash < Hash
def ==(x)
y = Hash[self.sort]
puts y.class
puts y
x = Hash[x.sort]
puts x.class
puts x
puts "______"
xkeys = MyArray.new(x.keys)
puts xkeys.class
puts xkeys.to_s
puts xkeys.object_id

puts xkeys[0].class
puts xkeys[0]
puts xkeys[0].object_id
puts "______"
xvals = MyArray.new(x.values)
puts "______"
selfkeys = MyArray.new(y.keys)
puts selfkeys.class
puts selfkeys.to_s
puts selfkeys.object_id

puts selfkeys[0].class
puts selfkeys[0]
puts selfkeys[0].object_id
puts "______"
selfvals = MyArray.new(y.values)
puts xkeys.==(selfkeys)
puts xvals.==(selfvals)
end
end


a1 = MyHash[{"one" => 1, "two" => 2}]
b1 = MyHash[{"one" => 1, "two" => 2}]
puts a1.==(b1)

得到

Hash
{"one"=>1, "two"=>2}
Hash
{"one"=>1, "two"=>2}
______
MyArray
["one", "two"]
21638020
String
one
21641920
______
______
MyArray
["one", "two"]
21637580
String
one
21641920
______
true
true

正如您从结果中看到的那样,具有相同值“one”的 2 个 String 对象具有相同的 object_id 21641920,而它应该具有不同的 ID。那么谁能给我一些提示或告诉我在这种情况下如何获得不同的 ID?最好的问候。

最佳答案

String 对象用作 Hash 中的键时,哈希将复制并在内部卡住字符串,并将该副本用作其键。

引用:Hash#store .

关于ruby - 为什么 2 个相同的字符串在 Ruby 中具有相同的 object_id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30138873/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com