Upload
sangyong-sim
View
46
Download
0
Embed Size (px)
Citation preview
How this works?
x = 'key'hash = {}hash[x] = 'value'
x.object_id == hash.keys.first.object_id>> false
How this works?
x = 'key'hash = {}# 주의: 예시코드입니다.# temp_x = x.dup# temp_x.freeze# hash[temp_x] = 'value'
x = 'string'y = {}y[x] = :valuex = 'str'y.key? x>> false
…str :value…
x = 'string'y = {}y[x] = :valuex = 'str'y.key? 'str'>> true
…string :value
…
class Foo attr_accessor :hashend
x = Foo.newx.hash = 10hsh = {}hsh[x] = :hello
hash: key를 이용해 주소를 계산하고 반환 eql?: 해시값이 충돌하는지 확인
KEY HASH VALUE
x 10 :hello11
x.hash = 11
puts hsh.key?(x)>> falseputs hsh.keys.include?(x)>> true
KEY HASH VALUE
10 :hellox 11
x.hash = 11hash.rehashputs hash.key?(x)>> trueputs hash.keys.include?(x)>> true
KEY HASH VALUE
x 10 :hello11
KEY HASH VALUE
10x 11 :hello
Calculating ------------------------------------- Hash#dup 7.705k i/100ms Hash[] 15.978k i/100ms --------------------------------------------------- Hash#dup 93.648k (± 4.9%) i/s - 470.005k Hash[] 230.497k (±11.2%) i/s - 1.150M