h = {}
# like an array, you can also store things by number
h[0] = "cero"
h[0]
# => "cero"
h["hello"] = "hola"
h["hello"]
# => "hola"
h["hello"] = "hola" ^ key
h["hello"] = "hola" ^ value
Terminology:
We mapped the key "hello"
to the value "hola"
In other words
the key "hello" maps to the value"hola"
# Hashes use square brakets too!
# Correct:
h["hello"] = "hola"
# WRONG:
h{"hello"} = "hola"
h{"hello" = "hola"}
# Correct:
h["hello"] = "hola"
1. Create a new hash.
2. then map the key "thank you" to the value "gracias"?
If you finish early, try experimenting mapping other things.
And ask me questions while you work!
Please also work in your_name.rb file on Stypi
Set Timer
h["thank you"] = "gracias"
h["thank you"]
# => "gracias"
# What's "hello" in Spanish?
h["hello"]
# => "hola"
# What's "thank you" in Spanish?
h["thank you"]
# => "gracias"
h
# => {0 => "cero", "hello" => "hola", "thank you" => "gracias"}
# The '=>' symbol
h["friend"] = "voldemort" # oops...
h["friend"] = "amigo"
h["friend"]
# => "amigo"
h
# => {0=>"cero", "hello"=>"hola", "thank you"=>"gracias", "friend"=>"amigo"}
h.delete("friend")
h["friend"]
# => nil
h
# => {0=>"cero", "hello"=>"hola", "thank you"=>"gracias"}
array = ["apple", "bannana", "cherry"]
h = {0 => "cero", :hello=>"hola", "thank you"=>"gracies"}
h[:hello]
# => "hola"
# Symbols used as hashes for keys a lot because they are immutable
{:some_key => "some value"}
# Shortcut Notation
{:some_key => "some value"} == {some_key: "some value"
External APIS: Facebook Graph | [UrlOpen!]
user_data = { id: "845160715", name: "Jonathan Leung", first_name: "Jonathan", last_name: "Leung", username: "FriendJonathanLeung", gender: "male", locale: "en_US" }
user_data[:name]
# => "Jonathan Leung"
array = ["845160715", "Jonathan Leung", "Jonathan", "Leung", "FriendJonathanLeung", "male", "en_US"]
array[1]
# => "Jonathan Leung"
# This is NOT intuitive
# What if they want to add or remove a property?!
array = []
array[2] = "charger"
array[22] = "wallet"
array
# => [nil, nil, "charger", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, "wallet", nil, nil, nil]
user_data = { id: "845160715", name: "Jonathan Leung", first_name: "Jonathan", last_name: "Leung", username: "FriendJonathanLeung", gender: "male", locale: "en_US" }
users = ["Mark Zuckerberg", "Chris Hughes", "Dustin Moskovitz"]
users[1]
# ?
# => "Chris Hughes"
users[2]
# ?
# => "Dustin Moskowitz"
users = ["Mark Zuckerberg", "Chris Hughes", "Dustin Moskovitz"]
users.delete("Chris Hughes")
users[2]
# ?
# => nil
users
# => ["Mark Zuckerberg", "Dustin Moskovitz"]
users[1]
# => "Dustin Moskovitz"
# => ["Mark Zuckerberg", "Dustin Moskovitz"] ^ 0th 1st
users = { 0=>"Mark Zuckerberg", 1=>"Chris Hughes", 2=>"Dustin Moskovitz" }
users[1]
# ?
# => "Chris Hughes"
users[2]
# ?
# => "Dustin Moskowitz"
users = { 0=>"Mark Zuckerberg", 1=>"Chris Hughes", 2=>"Dustin Moskovitz" }
users.delete("Chris Hughes")
users[1]
# => nil
users[2]
# "Dustin Moskovitz"
# => nil
users
users = { 0=>"Mark Zuckerberg", 2=>"Dustin Moskovitz" }
# Cafeteria Line
line = ["Mark Zuckerberg", "Chris Hughes", "Dustin Moskovitz"]
line[0]
# => "Mark Zuckerberg"
line[1]
# => "Chris Hughes"
# Zuckerberg pays for his meal, and leaves the line
line.delete_at(0)
line
["Chris Hughes", "Dustin Moskovitz"]
line[0]
# => "Chris Hughes"