Hashes and Symbols

TBD

Start[missing "en.views.course_landing_page.learn-ruby.course_illustration" translation]
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 associated with some value. For example: ...

  2. 2

    We can also iterate over hashes using the [...] method. For example, we could do [...] This will print out a list of keys and values from [...] , each on its own line.

  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 [...] . Along with ...

  4. 4

    You don't have to settle for [...] as a default value, however. If you create your hash using the [...] syntax, you can specify a default like so: [...] Now if you try to access a nonexisten...

  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: [...] Above and beyond the different syntax, there's a key behavior of symbols that make...

  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 o...

  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.) [...] ...

  9. 9

    Converting between strings and symbols is a snap. [...] The [...] and [...] 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 [...] , you can also use [...] . 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. [...] This is because the [...] looks like a tiny rocket...

  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 [...] . [...] ...

  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. [...] 1. A hash is ...

  3. 3

    Good work! Now we'll want to create the main body of our program: the [...] statement, which will decide what actions to take based on what the user types in. [...] and [...] are powerful, b...

  4. 4

    Great! Let's build out each part of the [...] , 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 [...] . 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 ...

  7. 7

    Perfect! Let's move on to the next branch of our [...] 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...

  8. 8

    Awesome! Now let's handle displaying the contents of our [...] 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 [...] statement, which will remove whatever key the user specifies from the hash. (This will be very similar to what we did for "add" and "upda...

  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...

Hashes and Symbols

Start[missing "en.views.course_landing_page.learn-ruby.course_illustration" translation]