• Shuffle
    Toggle On
    Toggle Off
  • Alphabetize
    Toggle On
    Toggle Off
  • Front First
    Toggle On
    Toggle Off
  • Both Sides
    Toggle On
    Toggle Off
  • Read
    Toggle On
    Toggle Off
Reading...
Front

Card Range To Study

through

image

Play button

image

Play button

image

Progress

1/43

Click to flip

Use LEFT and RIGHT arrow keys to navigate between flashcards;

Use UP and DOWN arrow keys to flip the card;

H to show hint;

A reads text to speech;

43 Cards in this Set

  • Front
  • Back

any?[{|(key,value)| block}]

returns true or false

assoc(obj)

searches through the hash comparing obj with the key using ==.




Returns the key-value pair (two elements array) or nil if no match found.




h = {"colors" => ["red", "blue", "green"], "letters" => ["a", "b", "c" ]}




h.assoc("letters") #=> ["letters", ["a", "b", "c"]]




h.assoc("foo") #=> nil

clear

removes all key-value pairs from the hash

compare_by_identity

makes the hash compare its keys by their identity, ie it will consider the exact same objects as keys



h1 = { "a" => 100, "b" => 200, :c => "c" }




h1["a"] #=> 100




h1.compare_by_identity




h1.compare_by_identity? #=> true




h1["a".dup] #=> nil # different objects.




h1[:c] #=> "c" # same symbols are all same.


compare_by_identity?

Returns true if hsh will compare its keys by theiridentity.



default(key=nil)

returns the default value, the value that would be returned if key did not exist.




h = Hash.new #=> {}h.default #=> nil




h.default(2) #=> nilh = Hash.new("cat") #=> {}




h.default #=> "cat"




h.default(2) #=> "cat"h = Hash.new {|h,k| h[k] = k.to_i*10} #=> {}




h.default #=> nil




h.default(2) #=> 20

default

sets the default value, the value returned for a key that does not exist in a hash.




Not possible to set the default to a proc to be executed on each key lookup.




h = { "a" => 100, "b" => 200 }




h.default = "Go fish"




h["a"] #=> 100




h["z"] #=> "Go fish"




# This doesn't do what you might hope...




h.default = proc do |hash, key| hash[key] = key + key




end




h[2] #=> #




>

default_proc

If Hash::new was invoked with a block, return that block,otherwise return nil.




h = Hash.new {|h,k| h[k] = k*k } #=> {}




p = h.default_proc #=> #




>







>

default_proc = proc_obj or nil

Sets the default proc to be executed on each failed key lookup.




h.default_proc = proc do |hash, key| hash[key] = key + key




end




h[2] #=> 4




h["cat"] #=> "catcat"

delete(key)




delete(key){|key|,block}

Deletes the key-value pair and returns the value from hsh whosekey is equal to key. If the key is not found, returns thedefault value. If the optional code block is given and the key isnot found, pass in the key and return the result of block.




h = { "a" => 100, "b" => 200 }




h.delete("a") #=> 100




h.delete("z") #=> nil




h.delete("z") { |el| "#{el} not found" } #=> "z not found"

delete_if{|key,value|block}




delete_if

deletes every key-value pair from hash for which the block evaluates to true




h = { "a" => 100, "b" => 200, "c" => 300 }




h.delete_if {|key, value| key >= "b" } #=> {"a"=>100}

each{|key,value|block}


each_pair{|key,value|block}




each


each_pair

calls block once for each key in the hash, passing the key-value pair as parameters




h = { "a" => 100, "b" => 200 }




h.each {|key, value| puts "#{key} is #{value}" }




#> a is 100


#> b is 200

each_key{|key|block}


each_key

calls block once for each key in the hash, passing the key as a parameter




h = { "a" => 100, "b" => 200 }h.each_key {|key| puts key }




#>a


#>b

each_pair{| key, value | block }


each_pair

Calls block once for each key in the hash, passing thekey-value pair as parameters




h = { "a" => 100, "b" => 200 }




h.each {|key, value| puts "#{key} is #{value}" }




#> a is 100


#> b is 200

each_value{|value|block}


each_value

calls the block once fore each key in the hash, passing the value as the parameter




h = { "a" => 100, "b" => 200 }




h.each_value {|value| puts value }




#>100


#>200

empty?

returns true if the hash contains no key-value pairs

eql?(other)

returns if the hash and other are both hashes with the same content

fetch(key,[default])


fetch(key){|key|block}

Returns a value from the hash for the given key.




(fUse this versus #[] when you want to supply a default behavior for the case when the key doesn't exist.)




If the key can’t be found,there are several options: With no other arguments, it will raise anKeyError exception; if default is given, then thatwill be returned; if the optional code block is specified, then that willbe run and its result returned.




h = { "a" => 100, "b" => 200 }




h.fetch("a") #=> 100




h.fetch("z", "go fish") #=> "go fish"




h.fetch("z") { |el| "go fish, #{el}"} #=> "go fish, z"





flatten


flatten(level)

returns a new array that is a 1-D flattening of the hash. Thatis, for every key or value that is an array, extract its elements into thenew array. Unlike Array#flatten,this method does not flatten recursively by default. The optionallevel argument determines the level of recursion to flatten.




a = {1=> "one", 2 => [2,"two"], 3 => "three"}




a.flatten # => [1, "one", 2, [2, "two"], 3, "three"]a.flatten(2) # => [1, "one", 2, 2, "two", 3, "three"]

has_key?(key)

returns true if the given key is present in the hash



see also #include?




h = { "a" => 100, "b" => 200 }




h.has_key?("a") #=> true




h.has_key?("z") #=> false

has_value?(value)

returns true if the given value is present for some key in the hash




h = { "a" => 100, "b" => 200 }




h.has_value?(100) #=> true




h.has_value?(999) #=> false

include?(key)

returns true if the given key is present in the hash



see also #has_key?




h = { "a" => 100, "b" => 200 }




h.has_key?("a") #=> true




h.has_key?("z") #=> false


to_s


inspect

returns the contents of the hash as a string







h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }




h.to_s #=> "{\"c\"=>300, \"a\"=>100, \"d\"=>400}"


invert

returns a new hash created by using the hash's values as keys and the keys as values



remember that a hash can only have one of each key, so duplicates will be removed.




h = { "n" => 100, "m" => 100, "y" => 300, "d" => 200, "a" => 0 }




h.invert #=> {0=>"a", 100=>"m", 200=>"d", 300=>"y"}




*note the "n" key/value pair is missing in the result


keep_if{|key,value| block}




keep_if

deletes every key-value pair from the hash for which the block evaluates to false

key(value)

returns the key of an occurrence of a given value.




I'd call it "key with value"




h = { "a" => 100, "b" => 200, "c" => 300, "d" => 300 }




h.key(200) #=> "b"




h.key(300) #=> "c"




h.key(999) #=> nil

key?(key)

returns true if the given key is present in the hash



h = { "a" => 100, "b" => 200 }




h.has_key?("a") #=> true




h.has_key?("z") #=> false



*not sure why the definition uses just key? but the example uses has_key?

keys

returns a new array populated with the keys from this hash.



see also hash#values




h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 }




h.keys #=> ["a", "b", "c", "d"]

length

returns the number of key-value pairs in the hash




h = { "d" => 100, "a" => 200, "v" => 300, "e" => 400 }




h.length #=> 4




h.delete("a") #=> 200




h.length #=> 3

merge(other_hash)


merge(other_hash){|key,oldval,newval|block}




merge!(other_hash)


merge!(other_hash){|key,oldval,newval|block}

Returns a new hash containing the contents of other_hash and thecontents of hsh. If no block is specified, the value for entrieswith duplicate keys will be that of other_hash. Otherwise thevalue for each duplicate key is determined by calling the block with thekey, its value in hsh and its value in other_hash.




h1 = { "a" => 100, "b" => 200 }




h2 = { "b" => 254, "c" => 300 }




h1.merge(h2) #=> {"a"=>100, "b"=>254, "c"=>300}




h1.merge(h2){|key, oldval, newval| newval - oldval} #=> {"a"=>100, "b"=>54, "c"=>300}




h1 #=> {"a"=>100, "b"=>200}

rassoc(obj)

searches through the hash comparing obj with the values using ==. Returns the first key-value pair (two-element array) that matches).





a = {1=> "one", 2 => "two", 3 => "three", "ii" => "two"}




a.rassoc("two") #=> [2, "two"]




a.rassoc("four") #=> nil

rehash

Rebuilds the hash based on the current hash values for each key. If valuesof key objects have changed since they were inserted, this method willreindex hsh. If Hash#rehash is called while aniterator is traversing the hash, an RuntimeError will beraised in the iterator.




a = [ "a", "b" ]




c = [ "c", "d" ]




h = { a => 100, c => 300 }




h[a] #=> 100




a[0] = "z"




h[a] #=> nil




h.rehash #=> {["z", "b"]=>100, ["c", "d"]=>300}




h[a] #=> 100

reject{|key,value|block}




reject!{|key,value|block}

returns a new hash consisting of entries for which the block returns false



seems like the opposite of select




h = { "a" => 100, "b" => 200, "c" => 300 }




h.reject {|k,v| k < "b"} #=> {"b" => 200, "c" => 300}




h.reject {|k,v| v > 100} #=> {"a" => 100}


replace(other_hash)

replaces the contents of the hash with the contents of other_hash



basically clears the original and replaces it with the new.






h = { "a" => 100, "b" => 200 }




h.replace({ "c" => 300, "d" => 400 }) #=> {"c"=>300, "d"=>400}

select{|key,value|block}




select!{|key,value|block}





returns a new hash consisting of entries for which the block returns true.




seems like the opposite of reject




h = { "a" => 100, "b" => 200, "c" => 300 }




h.select {|k,v| k > "a"} #=> {"b" => 200, "c" => 300}




h.select {|k,v| v < 200} #=> {"a" => 100}

shift

removes a key-value pair from the hash and returns it as the two-item array, or the hash's default if the value in the hash is empty.





h = { 1 => "a", 2 => "b", 3 => "c" }




h.shift #=> [1, "a"]




h #=> {2=>"b", 3=>"c"}

size

returns the number of key-value pairs in the hash




h = { "d" => 100, "a" => 200, "v" => 300, "e" => 400 }




h.length #=> 4




h.delete("a") #=> 200




h.length #=> 3

h.store

associates a value to a key



seems like a different way to add a key/value pair to a hash.




h = { "a" => 100, "b" => 200 }




h["a"] = 9




h["c"] = 4




h #=> {"a"=>9, "b"=>200, "c"=>4}




h.store("d", 42) #=> 42




h #=> {"a"=>9, "b"=>200, "c"=>4, "d"=>42}




Shouldn't work if a key is frozen.





to_a

converts the hash to a nested array of [k,v] arrays




h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300 }




h.to_a #=> [["c", 300], ["a", 100], ["d", 400]]





to_h




to_hash

to_h returns self, but if it's called on a subclass of hash, it will convert the receiver to a hash object



to_hash returns self


update(other_hash)




update(other_hash){|key,oldval,newval|blocl}

Adds the contents of other_hash to hsh. If no block isspecified, entries with duplicate keys are overwritten with the values fromother_hash, otherwise the value of each duplicate key isdetermined by calling the block with the key, its value in hsh andits value in other_hash.

value?(value)

returns true if the given value is present for some key in the hash




h = { "a" => 100, "b" => 200 }




h.has_value?(100) #=> true




h.has_value?(999) #=> false

values_at(key,...)

return an array containing the value associated with the given keys




also see #hash.select




h = { "cat" => "feline", "dog" => "canine", "cow" => "bovine" }




h.values_at("cow", "cat") #=> ["bovine", "feline"]