Learn
Hashes and Symbols
Converting Between Symbols and Strings

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!

Instructions

1.

We have an array of strings we’d like to later use as hash keys, but we’d rather they be symbols.

  1. Create a new variable, symbols, and store an empty array in it.
  2. Use .each to iterate over the strings array.
  3. For each s in strings, use .to_sym to convert s to a symbol and use .push to add that new symbol to symbols.
  4. Print the symbols array.

Check the hint for a refresher on .each and .push.

Folder Icon

Take this course for free

Already have an account?