tl;dr
*JSONView is a great way to view raw json files. *Evernote for notesSymbol [for hash keys] functions differently in memory compared to strings. Ruby maintains that memory location directly thus making it more efficient.
mkdir -p ~/Code/wk1d4/
git clone https://gist.github.com/5825156.git ~/Code/wk1d4/flickr
pushd ~/Code/wk1d4/flickr
To open a file in sublime directly from Terminal: subl ./ga-flickr.rb
Whenever you get a file over the network or webpage, it's going to be strings.
Today's flickr homework assignment: https://gist.github.com/coolaj86/5825156
.inject
iterates through all the variables and returns a value in the end which is a sum of all the operations in the previous variables. It provides a shortcut for loops. It is an enumerable and works on arrays and hashes (and range, too!).
#Example 1------
array = [1,2,3,4,5]
array.inject { |sum, i| sum + 1 }
# on first pass, sum= nil, i= array[0] = 1, so it returns nil + 1
# and coerces nil to 0, yielding 0+1 = 1
# on second pass, sum = 1, i= array[1] = 2, so it returns 1+2 = 3
# on third pass, sum = 3, i= array[2] = 3, returning 3 + 3 = 6
#.. and so on, returning 15 as a num
--------------------------------------------------------------------
#Example 2------
[9] pry(main)> arr = ["Kris", "Lauren", "ian", "Brye"]
=> ["Kris", "Lauren", "ian", "Brye"]
[10] pry(main)> arr.inject do |all, one|
[10] pry(main)* all + ", #{one}"
[10] pry(main)* end
#=> "Kris, Lauren, ian, Brye"
--------------------------------------------------------
#Example 3------ from lab.rb in class
puts hash["items"].inject("") { |all, item|
all + item["author"]
}
#the ("") after inject defaults 'all' to " " on the first pass, making
#the output default to a string.
`do/end` block statement bound to puts (assuming puts is accepting a block)
puts (hash["items"].inject("") { |all, item|
all + item["author"])
`{}` bound to inject
puts (hash["items"].inject("")) { |all, item|
all + item["author"]
Math:
n**^(1/n) nth-root as in 2nd-root = square root Math.sart(n) #argument
What if you don't want to call an argument?
Functional style: has an argument passed through.
Class style: you can have a thing do work on itself (without an argument) and return a value.
=> 'Self'
require ("./contact")
the ./
part asks to look for the file contact in the current folder that you are in, or the folder where your current file where this line is written is saved. Ruby automatically knows it is looking for a .rb file, so you don't need to write the file extention.
@name - an instance variable self -
when we call insepct_contact on something, we create an instance of contact. Doing self.name you're running the method on it.
#Classes:
classes are similar to hashes, but you can't assign a fcn to a hash.
We've installed Sublime Linter through sublime package Control, this will give us error check while we type: http://wbond.net/sublime_packages/package_control/installationhttps://github.com/SublimeLinter/SublimeLinter
*when you create something, you need a way to set the variable to a default value-- initialize
when you create a a = class.new anything that has self in front of it is an instance method
the class is the self.. it is not the instance that is the self.
Until you initialize, you don't have anything variables that are instance variables.. which is what .new does, it creates a new instance of the class which uses these variables.