Setting Up!



Setting Up!

0 0


learning-ruby-hashes


On Github jonleung / learning-ruby-hashes

Setting Up!

Open broadcast.rb.

And then move it in a new window here.

Open Stypi.

And select your_username.rb

Move this remote controlled presentation window to the top right window.

Open a copy of the presentation that is not remote controlled.

(so you can go at your own pace)

Open Url Open and put it in the top left window.

Switch your focus back to broadcast.rb

Switch your focus back to Remote Presentation

Setup Complete!

^_^ Questions!?

Hashes

h = {}
# like an array, you can also store things by number
h[0] = "cero"
h[0]
# => "cero"
h["hello"] = "hola"
h["hello"]
# => "hola"

Terminology

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"

A Quick Note:

# Hashes use square brakets too!
# Correct:
h["hello"] = "hola"
# WRONG:
h{"hello"} = "hola"
h{"hello" = "hola"}
# Correct:
h["hello"] = "hola"

^_^ Questions!?

Practice

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

Lets Do It Together

h["thank you"] = "gracias"
h["thank you"]
# => "gracias"

Wait, didn't we just make a english-spanish dictionary?

# 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

^_^ Questions!?

Modification & Deletion

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"}

^_^ Questions!?

Declaring Hashes In One Line

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"

Using Hashes Over Arrays

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?!

Problem 2: Filled with nil

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"
}

^_^ Questions!?

Problem 3: Array indexes can change

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"
}

^_^ Questions!?

Sometimes you want arrays

# 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"

When should I use an Array or a Hash?

It Depends!