DEV Community

Aizat Ibraimova
Aizat Ibraimova

Posted on

Hashes and Symbols

Codecademy Cheatsheet

Ruby Symbols

In Ruby, symbols are immutable names primarily used as hash keys or for referencing method names.

my_bologna = {
  :first_name => "Oscar",
  :second_name => "Meyer",
  :slices => 12
}

puts my_bologna[:second_name] # => Meyer

Enter fullscreen mode Exit fullscreen mode

Symbol Syntax
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, numbers, and underscores is allowed.

Make sure you don’t put any spaces in your symbol name—if you do, Ruby will get confused.

:my symbol # Don't do this!
:my_symbol # Do this instead.

Enter fullscreen mode Exit fullscreen mode

What are Symbols Used For?
Symbols pop up in a lot of places in Ruby, but they’re primarily used either as hash keys or for referencing method names.

sounds = {
  :cat => "meow",
  :dog => "woof",
  :computer => 10010110,
}
Enter fullscreen mode Exit fullscreen mode

Symbols make good hash keys for a few reasons:

  1. They’re immutable, meaning they can’t be changed once they’re created;
  2. Only one copy of any symbol exists at a given time, so they save memory;
  3. Symbol-as-keys are faster than strings-as-keys because of the above two reasons.

Ruby Hashes, Symbols, & Values

In Ruby hashes, key symbols and their values can be defined in either of two ways, using a => or : to separate symbol keys from values.

my_progress = {
  :program => "Codecademy",
  :language => "Ruby",
  :enthusiastic? => true 
}
#Key symbols and their values can be defined with a =>, also known as a hash rocket.

my_progress = {
  program: "Codecademy",
  language: "Ruby",
  enthusiastic?: true 
}
#Key symbols and their values can also be defined with the colon (:) at the end of the symbol followed by its value.
Enter fullscreen mode Exit fullscreen mode

Converting Between Symbols and Strings

Converting between strings and symbols is a snap.

:sasquatch.to_s
# ==> "sasquatch"

"sasquatch".to_sym
# ==> :sasquatch
Enter fullscreen mode Exit fullscreen mode

The .to_s and .to_sym methods are what you’re looking for!

strings = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]

symbols = []

strings.each do |s|
 symbols.push(s.to_sym)
end
print symbols
Enter fullscreen mode Exit fullscreen mode

Besides using .to_sym, you can also use .intern. This will internalize the string into a symbol and works just like .to_sym:

"hello".intern
# ==> :hello

Enter fullscreen mode Exit fullscreen mode

Ruby’s .to_sym method can convert a string to a symbol, and .to_i will convert a string to an integer.


Ruby .select Method
In Ruby, the .select method can be used to grab specific values from a hash that meet a certain criteria.

olympic_trials = {
  Sally: 9.58,
  John: 9.69,
  Bob: 14.91
}

olympic_trials.select { |name, time| time <  10.05 }
#The example above returns {:Sally=>9.58, :John=>9.69} since Sally and John are the only keys whose values meet the time < 10.05 criteria.

Enter fullscreen mode Exit fullscreen mode

Ruby .each_key & .each_value

In Ruby, the .each_key and .each_value methods are used to iterate over only the keys or only the values in a hash

my_hash = { one: 1, two: 2, three: 3 }

my_hash.each_key { |k| print k, " " }
# ==> one two three

my_hash.each_value { |v| print v, " " }
# ==> 1 2 3
Enter fullscreen mode Exit fullscreen mode

The Case Statement

if and else are powerful, but we can get bogged down in ifs and elsifs if we have a lot of conditions to check. Thankfully, Ruby provides us with a concise alternative: the case statement. The syntax looks like this:

case language
  when "JS"
    puts "Websites!"
  when "Python"
    puts "Science!"
  when "Ruby"
    puts "Web apps!"
  else
    puts "I don't know!"
end
Enter fullscreen mode Exit fullscreen mode

A NIGHT AT THE MOVIES

movies = {
  harry_potter: 4
}
puts "What would you like to do?"

choice = gets.chomp

case choice
 when "add"
   puts "What movie would you like to add?"
   title = gets.chomp
   if movies[title.to_sym].nil?
   puts "What rating does the movie have? (Type a number from 0 to 4)"
   rating = gets.chomp
   movies[title.to_sym] = rating.to_i
   puts "#{title} was added with a rating of #{rating}!"
   else 
   puts "That movie already exists! Its rating is #{movies[title.to_sym]}'"
   end
 when "update"
   puts 'What movie would you like to update?'
   title = gets.chomp
   if movies[title].nil?
   puts "There has been an error! Please check the spelling again."
   else
   puts "What is the new rating?"
   rating = gets.chomp
   movies[title.intern] = rating.to_i
   puts "#{title} has been updated with new rating of #{rating}."
   end
 when "display"
   movies.each { |movie, rating| 
   puts "#{movie}: #{rating}"}
 when "delete"
   puts "What is the title of the movie you would like to delete?"
   title = gets.chomp
   if movies[title.intern].nil?
   puts "The movie was not found"
   else
   movies.delete(title.to_sym)
   puts "#{title} has been removed."
   end
 else 
 puts "Error!"
end 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)