You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# @param [Hash, nil] options options to set the :initial_capacity or :load_factor. Ignored on some Rubies.
132
+
# @param [Proc] default_proc Optional block to compute the default value if the key is not set, like `Hash#default_proc`
133
+
definitialize(options=nil, &default_proc)
134
+
ifoptions.kind_of?(::Hash)
135
+
validate_options_hash!(options)
136
+
else
137
+
options=nil
138
+
end
133
139
134
-
# Get a value with key
135
-
# @param [Object] key
136
-
# @return [Object] the value
137
-
def [](key)
138
-
ifvalue=super# non-falsy value is an existing mapping, return it right away
139
-
value
140
-
# re-check is done with get_or_default(key, NULL) instead of a simple !key?(key) in order to avoid a race condition, whereby by the time the current thread gets to the key?(key) call
141
-
# a key => value mapping might have already been created by a different thread (key?(key) would then return true, this elsif branch wouldn't be taken and an incorrent +nil+ value
142
-
# would be returned)
143
-
# note: nil == value check is not technically necessary
144
-
elsif@default_proc && nil == value && NULL == (value=get_or_default(key,NULL))
145
-
@default_proc.call(self,key)
146
-
else
147
-
value
140
+
super(options)
141
+
@default_proc=default_proc
148
142
end
149
-
end
150
143
151
-
# Set a value with key
152
-
# @param [Object] key
153
-
# @param [Object] value
154
-
# @return [Object] the new value
155
-
def []=(key,value)
156
-
super
144
+
# Get a value with key
145
+
# @param [Object] key
146
+
# @return [Object] the value
147
+
def [](key)
148
+
ifvalue=super# non-falsy value is an existing mapping, return it right away
149
+
value
150
+
# re-check is done with get_or_default(key, NULL) instead of a simple !key?(key) in order to avoid a race condition, whereby by the time the current thread gets to the key?(key) call
151
+
# a key => value mapping might have already been created by a different thread (key?(key) would then return true, this elsif branch wouldn't be taken and an incorrent +nil+ value
152
+
# would be returned)
153
+
# note: nil == value check is not technically necessary
154
+
elsif@default_proc && nil == value && NULL == (value=get_or_default(key,NULL))
0 commit comments