Hash Quiz
animals = {
"silly" => "pugilistic kangaroo",
"sillier" => "kooky koala",
"silliest" => "Tiresius the neighborhood pterodactyl",
0 => "nihilophobia-inducing number"
}
What does animals[0]
return?
"pugilistic kangaroo"
"kooky koala"
"Tiresius the neighborhood pterodactyl"
"nihilophobia-inducing number"
nil
[key]
. In animals
, the key 0
corresponds to the value "nihilophobia-inducing number"
. Therefore animals[0]
returns "nihilophobia-inducing number"
.What does animals["unsilly"]
return?
"pugilistic kangaroo"
"kooky koala"
"Tiresius the neighborhood pterodactyl"
"nihilophobia-inducing number"
nil
nil
. animals
does not have the key "unsilly"
and lacks a default value. Therefore animals["unsilly"]
returns nil
.How might one add the value "Lucy the lucky lemur" to animals
?
animals.values << "Lucy the lucky lemur"
animals["Lucy uses her luck to justify her gambling addiction"] = "Lucy the lucky lemur"
animals["Lucy the lucky lemur"] = true
animals += "Lucy the lucky lemur"
animals["Lucy uses her luck to justify her gambling addiction"] = "Lucy the lucky lemur"
is therefore correct. The first choice appends the string "Lucy the lucky lemur"
to an array of the values of animals
, but it does not affect the hash animals
itself. The third choice adds "Lucy the Lucky lemur" as a key with the value true
. The syntax for the fourth option applies to array concatenation, among other things, and is inapplicable to hashes.What does animals.key?("silliest")
return?
["silly", "sillier", "silliest", 0]
["pugilistic kangaroo", "kooky koala", "Tiresius the neighborhood pterodactyl", "nihilophobia-inducing number"]
false
true
key?
method returns a boolean indicating whether its argument is a key in the hash. "silliest"
is a key in the hash animals
. Therefore animals.key?("silliest")
returns true
. What does animals.value?("boringest bear")
return?
["silly", "sillier", "silliest", 0]
["pugilistic kangaroo", "kooky koala", "Tiresius the neighborhood pterodactyl", "nihilophobia-inducing number"]
false
true
val?
method returns a boolean indicating whether its argument is a value in the hash. "boringest bear"
is not a value in the hash animals
. Therefore animals.value?("boringest bear")
returns false
. What does animals.keys
return?
["silly", "sillier", "silliest", 0]
["pugilistic kangaroo", "kooky koala", "Tiresius the neighborhood pterodactyl", "nihilophobia-inducing number"]
[["silly", "pugilistic kangaroo"], ["sillier", "kooky koala"], ["silliest", "Tiresius the neighborhood pterodactyl"], [0, "nihilophobia-inducing number"]]
true
keys
method returns an array of the keys of its receiver. ["silly", "sillier", "silliest", 0]
is therefore correct.What does animals.values
return?
["silly", "sillier", "silliest", 0]
["pugilistic kangaroo", "kooky koala", "Tiresius the neighborhood pterodactyl", "nihilophobia-inducing number"]
[["silly", "pugilistic kangaroo"], ["sillier", "kooky koala"], ["silliest", "Tiresius the neighborhood pterodactyl"], [0, "nihilophobia-inducing number"]]
true
values
method returns an array of the values of its receiver. ["pugilistic kangaroo", "kooky koala", "Tiresius the neighborhood pterodactyl", "nihilophobia-inducing number"]
is therefore correct.What does animals.length == 5
return?
["silly", "sillier", "silliest", 0]
["pugilistic kangaroo", "kooky koala", "Tiresius the neighborhood pterodactyl", "nihilophobia-inducing number"]
false
true
length
method returns the number of key-value pairs. animals
has four key-value pairs. Therefore animals.length == 5
returns false
. def top_two_silliest(hash)
hash.sort_by {|k, v| k.to_s.length}[-2..-1]
end
What does top_two_silliest(animals)
return?
["sillier", "silliest"]
["kooky koala", "Tiresius the neighborhood pterodactyl"]
{"sillier" => "kooky koala", "silliest" => "Tiresius the neighborhood pterodactyl"}
[["sillier", "kooky koala"], ["silliest", "Tiresius the neighborhood pterodactyl"]]
sort_by
returns a nested array of [key,
value] arrays in the order specified by its block. animals.sort_by {|k, v| k.to_s.length}
returns [[0, "nihilophobia-inducing number"], ["silly", "pugilistic kangaroo"], ["sillier", "kooky koala"], ["silliest", "Tiresius the neighborhood pterodactyl"]]
. top_two_silliest
returns the last two elements of this array: ["sillier", "kooky koala"], ["silliest", "Tiresius the neighborhood pterodactyl"]
. animals = Hash.new("All animals are silly by default.")
animals["Lately Lucy's been down on her luck"] = false
animals["unsilly"] #=> ?
What does animals["unsilly"]
return?
nil
"All animals are silly by default"
false
true
"All animals are silly by default."
as the argument to Hash.new sets "All animals are silly by default."
as the hash's default argument. Accessing an nonexistent key like "unsilly"
therefore returns "All animals are silly by default."
.