Hashes and Symbols
Learn more about Ruby’s _hash_ data structure including its various syntaxes, relationship with _symbols_, and the commonly used methods it
StartKey Concepts
Review core concepts you need to learn to master this subject
Ruby Symbols
Ruby Symbols
my_bologna = {
:first_name => "Oscar",
:second_name => "Meyer",
:slices => 12
}
puts my_bologna[:second_name] # => Meyer
#Symbols must be valid Ruby variable names and always start with a colon (:).
In Ruby, symbols are immutable names primarily used as hash keys or for referencing method names.
Hashes and Symbols
Lesson 1 of 2
- 1Recall that hashes are collections of key-value pairs, where a unique key is associate…
- 2We can also iterate over hashes using the .each method. For example, we could do my_hash.each do |key, value| puts my_hash[] end This will print out a list of keys and values from my_hash, each…
- 3What happens if you try to access a key that doesn’t exist, though? In many languages, you’ll get an error of some kind. Not so in Ruby: you’ll instead get the special value nil. Along with false…
- 4You don’t have to settle for nil as a default value, however. If you create your hash using the Hash.new syntax, you can specify a default like so: my_hash = Hash.new(“Trady Blix”) Now if you try…
- 5We can certainly use strings as Ruby hash keys; as we’ve seen, there’s always more than one way to do something in Ruby. However, the Rubyist’s approach would be to use symbols.
- 6You can think of a Ruby symbol as a sort of name. It’s important to remember that symbols aren’t strings: “string” == :string # false Above and beyond the different syntax, there’s a key behavior…
- 7Symbols always start with a colon (:). They must be valid Ruby variable names, so the first character after the colon has to be a letter or underscore (_); after that, any combination of letters, n…
- 8Symbols pop up in a lot of places in Ruby, but they’re primarily used either as hash keys or for referencing method names. (We’ll see how symbols can reference methods in a later lesson.) sounds =…
- 9Converting between strings and symbols is a snap. :sasquatch.to_s # ==> “sasquatch” “sasquatch”.to_sym # ==> :sasquatch The .to_s and .to_sym methods are what you’re looking for!
- 10Remember, there are always many ways of accomplishing something in Ruby. Converting strings to symbols is no different! Besides using .to_sym, you can also use .intern. This will internalize the s…
- 11The hash syntax you’ve seen so far (with the => symbol between keys and values) is sometimes nicknamed the hash rocket style. numbers = { :one => 1, :two => “two”, :three => 3, } This …
- 12However, the hash syntax changed in Ruby 1.9. Just when you were getting comfortable! The good news is that the changed syntax is easier to type than the old hash rocket syntax, and if you’re used…
- 13We mentioned that hash lookup is faster with symbol keys than with string keys. Here, we’ll prove it! The code in the editor uses some new syntax, so don’t worry about understanding all of it just…
- 14We know how to grab a specific value from a hash by specifying the associated key, but what if we want to filter a hash for values that meet certain criteria? For that, we can use .select. grades …
- 15Great work! We’ve often found we only want the key or value associated with a key/value pair, and it’s kind of a pain to put both into our block and only work with one. Can we iterate over just …