Ruby Hashes Flashcards

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

hash

A

A hash is a data structure that stores items by associated keys. This is contrasted against arrays, which store items by an ordered index. Entries in a hash are often referred to as key-value pairs. This creates an associative representation of data.

a hash is created using symbols as keys and any data types as values. All key-value pairs in a hash are surrounded by curly braces {} and comma separated.

irb :001 > old_syntax_hash = {:name => ‘bob’}
=> {:name=>’bob’}

The newer syntax is introduced in Ruby version 1.9 and is much simpler. As you can see, the result is the same.

irb :002 > new_hash = {name: ‘bob’}
=> {:name=>’bob’}

many key-value pairs.

irb :003 > person = { height: ‘6 ft’, weight: ‘160 lbs’ }
=> {:height=>’6 ft’, :weight=>’160 lbs’}

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

add to hash

A

irb :003 > person = { height: ‘6 ft’, weight: ‘160 lbs’ }
=> {:height=>’6 ft’, :weight=>’160 lbs’}

irb :004 > person[:hair] = ‘brown’
=> “brown”
irb :005 > person
=> {:height=>’6 ft’, :weight=>’160 lbs’, :hair=>’brown’}

irb :006> person[:age] = 62
=> 62
irb :007> person
=> {:height=>’6 ft’, :weight=>’160 lbs’, :hair=>’brown’, :age=>62}

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

remove from a hash

A

irb :008 > person.delete(:age)
=> 62
irb :009 > person
=> {:height=>’6 ft’, :weight=>’160 lbs’, :hair=>’brown’}

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

retrieve info from a hash

A

irb :010 > person[:weight]

=> “160 lbs”

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

merge two hashes

A

irb :011 > person.merge!(new_hash)
=> {:height=>’6 ft’, :weight=>’160 lbs’, :hair=>’brown’, :name=>’bob’}

Notice that we used the bang operator (!) to make this change destructive. We could have chosen to use the merge method instead, which would have returned a new merged hash, but left the original person hash unmodified.

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

.empty?

A

a method to detect whether something has any values or not.

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

Hashes vs. Arrays

A

When deciding whether to use a hash or an array, ask yourself a few questions:

Does this data need to be associated with a specific label? If yes, use a hash. If the data doesn’t have a natural label, then typically an array will work fine.

Does order matter? If yes, then use an array. As of Ruby 1.9, hashes also maintain order, but usually ordered items are stored in an array.

Do I need a “stack” or a “queue” structure? Arrays are good at mimicking simple “first-in-first-out” queues, or “last-in-first-out” stacks.

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

has_key? method

A

The has_key? method allows you to check if a hash contains a specific key. It returns a boolean value.

irb :001 > name_and_age = { “Bob” => 42, “Steve” => 31, “Joe” => 19}
=> {“Bob”=>42, “Steve”=>31, “Joe”=>19}
irb :002 > name_and_age.has_key?(“Steve”)
=> true
irb :003 > name_and_age.has_key?(“Larry”)
=> false

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

select method

A

The select method allows you to pass a block and will return any key-value pairs that evaluate to true when ran through the block.

irb :004 > name_and_age.select { |k,v| k == “Bob” }
=> {“Bob”=>42}
irb :005 > name_and_age.select { |k,v| (k == “Bob”) || (v == 19) }
=> {“Bob”=>42, “Joe”=>19}

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

fetch method

A

The fetch method allows you to pass a given key and it will return the value for that key if it exists. You can also specify an option for return if that key is not present.

irb :006 > name_and_age.fetch("Steve")
=> 31
irb :007 > name_and_age.fetch("Larry")
=> KeyError: key not found: "Larry"
     from (irb):32:in `fetch'
     from (irb):32
     from /usr/local/rvm/rubies/ruby-2.0.0-rc2/bin/irb:16:in `'
irb :008 > name_and_age.fetch("Larry", "Larry isn't in this hash")
=> "Larry isn't in this hash"
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

to_a method

A

The to_a method returns an array version of your hash when called. Let’s see it in action. It doesn’t modify the hash permanently though.

irb :009 > name_and_age.to_a
=> [[“Bob”, 42], [“Steve”, 31], [“Joe”, 19]]
irb :010 > name_and_age
=> {“Bob”=>42, “Steve”=>31, “Joe”=>19}

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

.keys or .values

A

if you want to just retrieve all the keys or all the values out of a hash

irb :0011 > name_and_age.keys
=> [“Bob”, “Steve”, “Joe”]
irb :0012 > name_and_age.values
=> [42, 31, 19]

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