Array and Hashes Flashcards

1
Q

how to access the 3rd element in an array?

A

array_name[2]

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

how to access a hash value?

A

hash_name[“key”]

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

create a new hash called pets, and add dog > daisy to it.

A
pets = Hash.new
pets["dog"] = "daisy"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

iterate through the array called my_array and the hash called my_hash.

A

my_array.each { |x| puts “#{x}” }

my_hash.each { |x, y| puts “#{x} : #{y}” }

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

how to access mina in this array: my_array = [[sara, mina], [fady, nancy]]

A

my_array[0][1]

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

how to create an empty hash with a default value?

A

my_hash = Hash.new(“The default value”)

so when you call a non existed key, you will get the default value.

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

sort a hash into an array of arrayes

A

my_array = my_hash.sort_by do |key, value|
value
end
my_array.reverse! #to order it higher to lower

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