Array and Hashes Flashcards
We can put many things in an array, heck we can even put arrays in an array, but what is this called?
This is called multidimensional arrays
Create a two dimensional array and print each array on it’s own separate line
my_array = [[‘Scott’],[’Scott’],[‘Scott’], [’Scott’]]
my_array.each { |word| puts “#{word}” }
What is a hash?
Create a has using literal notation, and then create a hash the other way (Also print a value from each to console)
Create a has using literal notation, and then create a hash the other way (Also print a value from each to console)
me = { “name” => “Scott”, age => 18, virgin => true } puts me[“name”]
pets = Hash.new
pets[“Lola”] = “rabbit”
puts pets[“Lola”]
Iterate over the following hash, but print out the value (only) to the console
my_hash = { "Mohit" => "Curry", "Seb" => "Burritos", "Scott" => "Kroket" }
my_has.each do |name, food|
puts food
end
If I have a two dimensional array, so an array that contains arrays, how could you print out the items in the sub arrays?
s = [[“ham”, “swiss”], [“turkey”, “cheddar”], [“roast beef”, “gruyere”]]
s.each do |sub_array| sub_array.each do |food| puts food end end
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?
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
Give three reasons why symbols are good to use as Hash keys?
- They’re immutable, meaning they can’t be changed once they’re created
- Only one copy of a symbol exists at any given time, so they save memory
- They’re faster, because of the above two reasons`
How would you convert the following array with strings to a new array with symbols? List the two methods for this conversion.
strings = ["JavaScript", "Ruby", "HTML", "CSS"] symbols = []
strings.each { |s| symbols.push(s.to_sym) }
print symbols
- to_sym
- .intern
Create a hash with old syntax and the new 1.9 Ruby syntax
Old:
movies = {
:elf => “Great”,
:Blind_Side => “Fantastic”
}
New:
movies = {
elf: “Great”,
Blind_Side: “Fantastic”
}
How would I print out just the key of an array, and just the values?
movies = { elf: "5", cars: "4" } movies.each_key { |k| puts k, " "} movies.each_value { |v| puts v, " "}
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?
arr. take(3)
arr. drop(3)
With the following array written below, remove all the elements based on true criteria
my_numbers = [2, 6, 5] odd_numbers = my_numbers.reject { |num| num % 2 == 0 }
How to check whether a hash pair has the value of a certain key or not?
movies[movie_name.to_sym].nil?
or
movies.include? movie_name.to_sym
How would you delete a key/value pair in the following hash?
movies {
Elf: 5,
Cars: 4
}
movies.reject! { |movie| movie == movie_name.to_sym }