Chevron Left Icon
Hashes and Symbols
Lesson 1 of 2
Chevron Right Icon
  1. 1
    Recall that hashes are collections of key-value pairs, where a unique key is associate…
  2. 2
    We 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…
  3. 3
    What 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…
  4. 4
    You 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…
  5. 5
    We 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.
  6. 6
    You 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…
  7. 7
    Symbols 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…
  8. 8
    Symbols 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 =…
  9. 9
    Converting 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!
  10. 10
    Remember, 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…
  11. 11
    The 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 …
  12. 12
    However, 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…
  13. 13
    We 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…
  14. 14
    We 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 …
  15. 15
    Great 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
  1. 1
    Keeping track of all the parts of our digital lives is a pain. Now that you know how to write Ruby, however, you can make things easy for yourself! Let’s start by creating a program that will keep …
  2. 2
    First things first: let’s create a hash to hold our movies and their ratings, and let’s prompt the user for input so we can eventually store movie/ratings pairs in our hash. favorite_foods = { ‘…
  3. 3
    Good work! Now we’ll want to create the main body of our program: the case statement, which will decide what actions to take based on what the user types in. if and else are powerful, but we can g…
  4. 4
    Great! Let’s build out each part of the case, one step at a time. We’ll start with the “add” branch.
  5. 5
    Perfect! Our program is really coming along. You might have wondered how we’re going to get our movies and ratings from the user—which come in as strings—into the hash where we want our movies to …
  6. 6
    All right! We’re nearly done with the “add” part of our case. The final thing we’ll want to do is perform a check to see whether the movie to be added is already in the hash. To do this, we’ll add…
  7. 7
    Perfect! Let’s move on to the next branch of our case statement, which handles updating an existing movie in the hash. (This should be very similar to the work we did in the “add” branch!) We’ll do…
  8. 8
    Awesome! Now let’s handle displaying the contents of our movies hash. This will be a little different from what we did for the “add” and “update” branches.
  9. 9
    Almost there! Let’s handle the “delete” part of our case statement, which will remove whatever key the user specifies from the hash. (This will be very similar to what we did for “add” and “update….
  10. 10
    Fantastic! You built a little app with only a few dozen lines of code. Impressive, isn’t it? The four verbs your program knows—add, display, update, and delete—are universal. This acronym is bette…