Hashes & symbols Flashcards
nil
returned if you try to access a hash key that does not exist.
Similar to false, but nil means nothing at all
symbols
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.
my_bologna = { :first_name => "Oscar", :second_name => "Meyer", :slices => 12 }
puts my_hash[:second_name] # => Meyer
.to_sym
converts a string to a sympol
.to_s
converts a symbol to a string
.intern
converts a string into a symbol
symbol syntax
symbol:
.select
used to filter for a hash that meets a specified criteria
grades = { alice: 100, bob: 92, chris: 95, dave: 97 }
grades.select { |name, grade| grade < 97 } # ==> { :bob => 92, :chris => 95 }
grades.select { |k, v| k == :alice } # ==> { :alice => 100 }
.each_key
iterates over only the keys of a hash
.each_value
iterates over only the values of a hash
when
acts as an If
i.e.
case choice when "add" puts "Added!" when "update" puts "Updated!" when "display" puts "Movies!" when "delete" puts "Deleted!" else puts "Error!" end
.to_i
converts a string to an integer
.delete
deletes a key/value from a hash
CRUD
acronym for create (add), display, update, delete.
Common actions you take when updating a database, asking a website for information, or writing a blog post.