Array and Hashes Flashcards

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

We can put many things in an array, heck we can even put arrays in an array, but what is this called?

A

This is called multidimensional arrays

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

Create a two dimensional array and print each array on it’s own separate line

A

my_array = [[‘Scott’],[’Scott’],[‘Scott’], [’Scott’]]

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

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

What is a hash?

A

Create a has using literal notation, and then create a hash the other way (Also print a value from each to console)

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

Create a has using literal notation, and then create a hash the other way (Also print a value from each to console)

A
me = {
  “name” => “Scott”,
   age => 18,
   virgin => true
}
puts me[“name”]

pets = Hash.new
pets[“Lola”] = “rabbit”
puts pets[“Lola”]

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

Iterate over the following hash, but print out the value (only) to the console

A
my_hash = {
"Mohit" => "Curry",
"Seb" => "Burritos",
"Scott" => "Kroket"
}

my_has.each do |name, food|
puts food
end

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

If I have a two dimensional array, so an array that contains arrays, how could you print out the items in the sub arrays?

A

s = [[“ham”, “swiss”], [“turkey”, “cheddar”], [“roast beef”, “gruyere”]]

s.each do |sub_array|
sub_array.each do |food|
puts food
end
end
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

With Hashes, you can assign keys to strings, but how can you assign a key as a symbol? Also write what the key behaviour difference is between the two?

A

my_hash = {
dog: “Woof”
}

The main difference is that if you have keys listed as a string then multiple of these keys can all have the same value, however with symbols theirs only one particular copy of a given symbol

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

Give three reasons why symbols are good to use as Hash keys?

A
  1. They’re immutable, meaning they can’t be changed once they’re created
  2. Only one copy of a symbol exists at any given time, so they save memory
  3. They’re faster, because of the above two reasons`
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How would you convert the following array with strings to a new array with symbols? List the two methods for this conversion.

A
strings = ["JavaScript", "Ruby", "HTML", "CSS"]
symbols = []

strings.each { |s| symbols.push(s.to_sym) }
print symbols

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

Create a hash with old syntax and the new 1.9 Ruby syntax

A

Old:

movies = {
:elf => “Great”,
:Blind_Side => “Fantastic”
}

New:

movies = {
elf: “Great”,
Blind_Side: “Fantastic”
}

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

How would I print out just the key of an array, and just the values?

A
movies = {
elf: "5",
cars: "4"
}
movies.each_key { |k| puts k, " "}
movies.each_value { |v| puts v, " "}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

If I have the following array, how could I access just the first three items in the array and how could I access everything BUT the first three?

A

arr. take(3)

arr. drop(3)

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

With the following array written below, remove all the elements based on true criteria

A
my_numbers = [2, 6, 5]
odd_numbers = my_numbers.reject { |num| num % 2 == 0 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How to check whether a hash pair has the value of a certain key or not?

A

movies[movie_name.to_sym].nil?

or

movies.include? movie_name.to_sym

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

How would you delete a key/value pair in the following hash?

movies {
Elf: 5,
Cars: 4
}

A

movies.reject! { |movie| movie == movie_name.to_sym }

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

With the following array how could you access the last element, not just simply wring out 5 for example because thats the last index of a length of 6. There are two ways, write both.

A

array[array.length - 1]

array[-1]