Hashes Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

How do you create a new hash called my_hash using the literal constructor?

A

my_hash = { }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you create a new hash called my_hash using the class constructor?

A

my_hash = Hash.new

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you retrieve the value “Pluto” in the following hash?

pets = {“cat” => “Maru”, “dog” => “Pluto”}

A

pets[“dog”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How would you add the key and value pair, “bird” and “Sammy in the following hash?

pets = {“cat” => “Maru”, “dog” => “Pluto”}

A

pets[“bird”] = “Sammy”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How would you reassign the value of the key “dog” from “Pluto” to “Koikoi” in the following hash?

pets = {“cat” => “Maru”, “dog” => “Pluto”}

A

pets[“dog”] = “Koikoi”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What does a symbol look like?

A

:this_is_what_a_symbol_looks_like

Symbols start with a colon.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between a string and a symbol?

A

Strings are mutable while symbols are not. Symbols cannot be modified after being created and will always stay the same size in memory.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you express the symbol :instructor with a value of “Isaac Newton” in a hash?

A

{ instructor: “Isaac Newton” }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you iterate over a hash using an each method?

A
hash.each do |key, value|
  #your code here
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What would the following code look like?

hash = {key1: “value1”, key2: “value2”}

hash.each do |key, value|
puts “#{key}: #{value}”
end

A

“key1: value1”

“key2: value2”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What value will the following code return?

birthday_kids = {
    "Timmy" => 9, 
    "Sarah" => 6, 
    "Amanda" => 27
}

birthday_kids.collect do |kids_name, age|
age * 7
end

A

[63, 42, 189]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you retrieve the string “Jasmine” in the following hash?

school = {
instructors: [“Cadence”, “Melody”, “Lryic”],
dev_team: [“Rose”, “Lily”, “Violet”, “Jasmine”],
students: [“Shelly”, “you”, “Sandy”, “Rocky”]
}

A

school[:dev_team][3]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How would you retrieve the string “you” in the following hash?

school = {
instructors: [“Cadence”, “Melody”, “Lyric”],
dev_team: [“Rose”, “Lily”, “Violet”, “Jasmine”],
students: [“Shelly”, “you”, “Sandy”, “Rocky”]
}

A

school[:students][1]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What value will the following code return?

groceries = {fruit: “Banana”, vegetable: “Broccoli”, dessert: “Cookie”}

groceries.values

A

[“Banana”, “Broccoli”, “Cookie”]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What value will the following code return?

groceries = {fruit: “Banana”, vegetable: “Broccoli”, dessert: “Cookie”}

groceries.keys

A

[:fruit, :vegetable, :dessert]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How would you return the values from the following hash?

groceries = {fruit: “Banana”, vegetable: “Broccoli”, dessert: “Cookie”}

A

groceries.values

17
Q

How would you return the keys from the following hash?

groceries = {fruit: “Banana”, vegetable: “Broccoli”, dessert: “Cookie”}

A

groceries.keys

18
Q

What value will the following code return?

alphabetical_order = {c: 100, d: 1}

alphabetical_order.min

A

[c:, 100]

c is lower than d in alphabetical order

19
Q

How would you return the key with the lowest value in the following hash?

alphabetical_order = {c: 100, d: 1}

A

alphabetical_order.min

20
Q

What value will the following code return?

groceries = {
  dairy: ["milk", "yogurt", "cheese"],
  vegetable: ["carrots", "broccoli", "cucumbers"],
  meat: ["chicken", "steak", "salmon"],
  grains: ["rice", "pasta"]
}

groceries.values

A

[[“milk”, “yogurt”, “cheese”], [“carrots”, “broccoli”, “cucumbers”], [“chicken”, “steak”, “salmon”], [“rice”, “pasta”]]

21
Q

What value will the following code return?

groceries = {
  dairy: ["milk", "yogurt", "cheese"],
  vegetable: ["carrots", "broccoli", "cucumbers"],
  meat: ["chicken", "steak", "salmon"],
  grains: ["rice", "pasta"]
}

groceries.values.flatten

A

[“milk”, “yogurt”, “cheese”, “carrots”, “broccoli”, “cucumbers”, “chicken”, “steak”, “salmon”, “rice”, “pasta”]

22
Q

What value will the following code return?

groceries = {
  dairy: ["milk", "yogurt", "cheese"],
  vegetable: ["carrots", "broccoli", "cucumbers"],
  meat: ["chicken", "steak", "salmon"],
  grains: ["rice", "pasta"]
}

groceries.values.flatten.min

A

“broccoli”