class Hash
Public Instance Methods
displace(key, value)
click to toggle source
Associates key
with value
, and returns key
's previously associated value. If key
had no previously associated value, returns +Hash#default+.
@example
visited = { "id1" => true, "id2" => false } visited.displace("id1", true) # == true visited # == { "id1" => true, "id2" => false } visited.displace("id2", true) # == false visited # == { "id1" => true, "id2" => true } visited.displace("id3", true) # == nil visited # == { "id1" => true, "id2" => true, "id3" => true }
@param key @param value @return [Hash]
# File lib/casual_support/hash/displace.rb, line 20 def displace(key, value) old_value = self[key] self[key] = value old_value end
put!(key, value)
click to toggle source
Associates key
with value
. Similar to +Hash#[]=+, but returns the Hash
instead of the value. Faster than +Hash#merge!+ for single updates in a loop.
@example
cache = id_list.reduce({}) do |hash, id| hash.put!(id, find_by_id(id)) end
@param key @param value @return [Hash]
# File lib/casual_support/hash/putbang.rb, line 15 def put!(key, value) self[key] = value self end