Last active
December 30, 2015 20:49
-
-
Save randalmaile/7883735 to your computer and use it in GitHub Desktop.
Hashes Checkpoint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##Hashes - Another type of collection | |
###Hashes store elements as key / value pairs | |
1. Keys are not restricted to exist as integers, in fact they can be any type of object, like a String or Symbol | |
2. Symbols are basically lightweight strings, only with a different syntax. Symbols have a : to the left of their name. So if we wanted to create a Symbol named "book", it would simply be :book | |
3. Symbols are very efficient objects used to retrieve values from a hash! | |
4. fruits[word] #=> returns "A red, yellow or green fruit." | |
fruits is the Hash and word is the argument passed to the get_definition method. word is also the Hash key we're looking for. | |
Example of a compact/readable hash: | |
``` | |
def get_definition(word) | |
fruits = { | |
apple: "A red, yellow or green fruit.", | |
banana: "A yellow fruit.", | |
broccoli: "A green vegetable." | |
} | |
p fruits[word] | |
end | |
``` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```Bloc output - setting attributes | |
User initialize can set just the name | |
User initialize can set all values | |
User initialize can set no values | |
``` | |
```Bloc output - Iterating over a Hash | |
hash_to_array returns array for short hash | |
hash_to_array returns array for longer hash | |
``` | |
```Bloc output - Hash Methods | |
merge_us merges two hashes that are unique | |
merge_us merges two hashes that are have some things in common | |
my_keys returns keys for a small hash | |
my_keys returns keys for a larger hash | |
do_i_have? returns false if it doesn't have any of the keys | |
do_i_have? returns false if one or more of the keys isn't in the hash | |
do_i_have? returns false if the hash has a different number of keys than the array | |
do_i_have? returns true if all keys are in the hash | |
do_i_have? returns true if all keys are in the hash, regardless of order | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. In the "Iterating over an array exercise", why do we create a new hash as an argument?: | |
def hash_to_array(h = {}) | |
a = [] | |
h.each do |key, value| | |
a << "#{key} is #{value}" | |
end | |
a | |
end | |
....you also see it here: | |
class User | |
attr_accessor :name, :email, :bio, :age, :sex | |
def initialize(config = {}) | |
@name = config[:name] || "n/a" | |
@email = config[:email] || "n/a" | |
@bio = config[:bio] || "n/a" | |
@age = config[:age] || "n/a" | |
@sex = config[:sex] || "n/a" | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#SETTING ATTRIBUTES | |
class User | |
attr_accessor :name, :email, :bio, :age, :sex | |
def initialize(config = {}) | |
@name = config[:name] || "n/a" | |
@email = config[:email] || "n/a" | |
@bio = config[:bio] || "n/a" | |
@age = config[:age] || "n/a" | |
@sex = config[:sex] || "n/a" | |
end | |
end | |
# Iterating over a Hash | |
def hash_to_array(h = {}) | |
a = [] | |
h.each do |key, value| | |
a << "#{key} is #{value}" | |
end | |
a | |
end | |
# Hash Methods | |
def merge_us(h1, h2) | |
h1.merge!(h2) | |
end | |
def my_keys(h) | |
h.keys | |
end | |
def do_i_have? (hash, array_of_keys) | |
if hash.keys.sort == array_of_keys.sort | |
true | |
else | |
false | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment