Instance Methods Flashcards
clear
Fn: Removes all key-value pairs from the hash
Returns: original hash
Syntax: my_hash.clear
invert
Fn: switch around the keys with the values
Returns: new hash
Syntax: my_hash.invert
#keys #values
Fn: create a new array with the keys/values of the hash as elements
Returns: a new array
Syntax: my_hash.keys(values)
values_at
Fn: returns the values associated with the given keys
Returns: an array of the values
Syntax: my_hash.values_at(key…)
update
Fn: Add the contents of another hash (overwriting with values from the new hash unless a block is specified)
Returns: original hash
Syntax: my_hash.update(new_hash)
#merge #merge!
Fn: Returns a hash containing values from both
Returns: a new hash/original hash (!)
Syntax: same as #update
has_key? / #key? / #member
Fn: returns true if the given key is present in hash
Returns: Boolean
Syntax: my_hash.hash_key(key)
has_value? / #value?
Fn: returns true if the hash contains the given value
Returns: Boolean
Syntax: same as #key?
#each_key #each_value
Fn: Calls the block once for each key, passing the key/value as a parameter
Returns: original hash
Syntax: my_hash.each_key{|key| block}
my_hash.each_value{|value| block}
each_pair
Fn: Calls the block once for each key in the hash, passing both key and value as parameters.
Returns: original hash
Syntax: my_hash.each_pair{|key, value| block}
eql?
Fn: returns true if both arguments are hashes with the same content
Returns: Boolean
Syntax: my_hash.eql?(other_hash)
to_s
Returns a string representation of the hash
length
Returns the number of key-value pairs in the hash
#delete_if #keep_if
Like the array versions, only there are parameters in the block for both key and value rather than just index.
Destructive.
flatten
Fn: Returns an array that is a one-dimensional flattening of the hash. Does not flatten recursively, just to the given level.
Returns: an array
Syntax: my_hash.flatten(level)
#empty? #include?
Like the array versions in that it returns true if the hash is empty or if the given object is included (as a key)
#any? #all?
Fn: Returns true if the given block is truthy for any or all elements.
Returns: Boolean
Syntax: my_hash.any?{|key, value| block}
#each / #each_pair #each_with_index
Fn: Calls the block once for each key in hash, passing the key and value (each/each_pair) or the key and index (hashes do have indices!)
Returns: original hash
Syntax: my_hash.each{|key, value(or index)| block}
#select #filter
Fn: returns an array containing elements for which the block returns true
Returns: new_ary
Syntax: my_hash.select{|key, value| block}
map
Fn: returns a new array with the results of running the block for each element in the hash
Returns: new_ary
Syntax: my_hash.map{|key, value| block}
each_with_object
Fn: iterates the block for each element, passing an arbitrary object which is then returned from the method.
Returns: object
Syntax: each_with_object(obj) { |(*args), memo_obj| … } → obj
partition
Fn: Split elements into those that are truthy or falsely for a given block
Returns: an array of two arrays - the first containing the elements for which the block returned true, the second for those where it returned false
Syntax: my_hash.partition{|obj| block}
#reject #reject!
Fn: returns a hash with all the elements of the original hash for which the block returns false
Returns: new hash or original (!) (or nil if no changes were made with !)
Syntax: my_hash.reject{|key, value| block}
replace
Fn: Replaces the contents of one hash with another’s
Returns: original hash
Syntax: my_hash.replace(other_hash)