Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created February 3, 2016 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/e90efc577610ce4e4341 to your computer and use it in GitHub Desktop.
Save JoshCheek/e90efc577610ce4e4341 to your computer and use it in GitHub Desktop.
Notes from representing nouns
# ----- Representing hashes with arrays -----
hash = {
'dog' => 'fighting',
'michael jordan' => 'jumping',
'army' => 'boxing'
}
array = [
['dog', 'fighting'],
['michael jordan', 'jumping'],
['army', 'boxing'],
]
array.find { |k, v| k == 'michael jordan' }[1] # => "jumping"
array.assoc('michael jordan')[1] # => "jumping"
array2 = [
["dog", "michael jordan", "army"],
["fighting", "jumping", "boxing"]
]
array2[1][array2[0].index('michael jordan')] # => "jumping"
# --------- Making our own strings ---------
# 36 = 'a'
# 37 = 'b'
# 38 = 'c'
# ...
"abc" # => "abc"
'a'.ord # => 97
'b'.ord # => 98
'c'.ord # => 99
'A'.ord # => 65
# ------- If we only had strings, how could we represent an array of strings? ----------
['abcd', 'lol']
['abc', 'def', 'ghi']
'(0)abc (1)def (2)ghi'
# ----------- Representing pwd ------------
# $ pwd
# /Users/josh/code/cp/daily/week1
"/Users/josh/code/cp/daily/week1"
['Users', 'josh', 'code', 'cp', 'daily', 'week1']
# ----------- The PATH variable -------------
# Which is an array of directories where I keep programs I want to run.
# $ echo "$PATH"
# /Users/josh/.opam/system/bin:/Users/josh/.gem/ruby/2.1.1/bin:/Users/josh/.rubies/ruby-2.1.1/lib/ruby/gems/2.1.0/bin:/Users/josh/.rubies/ruby-2.1.1/bin:/Users/josh/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Users/josh/code/dotfiles/bin:/Users/josh/bin:/Users/josh/.cabal/bin
[ "/Users/josh/.opam/system/bin",
"/Users/josh/.gem/ruby/2.1.1/bin",
"/Users/josh/.rubies/ruby-2.1.1/lib/ruby/gems/2.1.0/bin",
"/Users/josh/.rubies/ruby-2.1.1/bin",
"/Users/josh/bin",
"/usr/local/bin",
"/usr/bin",
"/bin:/usr/sbin",
"/sbin:/usr/local/bin",
"/opt/X11/bin",
"/Users/josh/code/dotfiles/bin",
"/Users/josh/bin",
"/Users/josh/.cabal/bin",
]
# --------- URL Params --------------
# http://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=the+little+book+of+talent
url=search-alias%3Daps
field-keywords=the+little+book+of+talent
language=en
# utm_content=buffer9387d&utm_medium=social&utm_source=twitter.com&utm_campaign=buffer
utm_content=buffer9387d
utm_medium=social
utm_source=twitter.com
utm_campaign=buffer
{ 'utm_content' => 'buffer9387d',
'utm_medium' => 'social',
'utm_source' => 'twitter.com',
'utm_campaign' => 'buffer',
}
# ---------- See how data will change in representation as we change requirements ------------
# student name
'James'
# student names
['James', 'Dongmin', 'Matthew']
# student names with their favourite colour
{ 'James' => 'Chocolate',
'Dongmin' => 'Just pick one',
'Matthew' => 'blue'
}
# student names with their favourite colour,
# by their military branch
[{'name' => 'James', 'colour' => 'Chocolate', 'branch' => 'Air Force'},
{'name' => 'Dongmin', 'colour' => 'Chocolate', 'branch' => 'Air Force'},
{'name' => 'Matthew', 'colour' => 'Chocolate', 'branch' => 'Air Force'},
]
{ "army" => {
'James' => 'Chocolate',
'Dongmin' => 'Just pick one',
},
"air-force" => {'Matthew' => 'blue'}
}["army"]
# => {"James"=>"Chocolate", "Dongmin"=>"Just pick one"}
# student names with their favourite colour,
# by their military branch
# and instructors with their state of residence, by their names.
{ 'students' => {
"army" => {
'James' => 'Chocolate',
'Dongmin' => 'Just pick one',
},
"air-force" => {'Matthew' => 'blue'}
},
"instructors" => { "Josh" => "CO",
"Andrea" => "OH",
"Rod" => "IL",
},
}['students']['army']['James']
# => "Chocolate"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment