Skip to content

Instantly share code, notes, and snippets.

@screamingmunch
Last active December 18, 2015 17:09
Show Gist options
  • Save screamingmunch/5816359 to your computer and use it in GitHub Desktop.
Save screamingmunch/5816359 to your computer and use it in GitHub Desktop.
GA- Ruby Arrays, Loops, Hashs

We spent most lecture today on arrays & hashes. AJ demonstrated creating a hash from a .csv file containing all the teaching staff's names and contact info. We had our first lab today as well, sorting through a json file of search results to manipulate the data and perform searches. I learned that you can create hashes within hashes, and even within arrays. For example: Items[3]["products"]["price"]. To find whether there is a match of a string (or regular expression) to your larger string (or hash/array/.json file), use: array["name"] =~ /Jay/

tl;dr Arrays, Loops, Hashes..

Getting files from Terminal:

j@ubuntu:~$ mkdir Code/wk1d3
j@ubuntu:~$ pushd Code/wk1d3/
j@ubuntu:~/Code/wk1d3$ wget https://gist.github.com/coolaj86/983abc67071fbb5d0143/
raw/0ae2430c4bcdb1574d7e392c0e0ab2c65dc08974/instructors.csv

wget retrieves the file from your url and saves it into your 
/Code/wk1d3 folder as "instructors.csv"

j@ubuntu:~/Code/wk1d3$ cat instructors.csv
running this will display the info in your csv file

Two tricks learned in Sublime Text:

ctrl / auto comments everything that's high-lighted

press ctrl D while one of the word is selected will automatically select the same word throughout the document.

Loops

```ruby for i in 1..1000 puts i if i % 7==0 end ------------------------------------------------- #-----Teach Staff Contact Info Hash----------# #--------------------------------------------# #!/usr/bin/env ruby require('pp') #pp = pretty print

magically load the csv string

def load() File.open("instructors.csv", "r") { |f| data = f.read() return data } end

def print_how_i_want(x) p(x, x.class) end

def fields_to_hash(fields) return { name: fields[0], email: fields[1], phone: fields[2], title: fields[3].gsub("(", '').gsub(")", '') } end

my whole program

def main() csvtext = load() persons = csvtext.split("\n") #splits csv into arrays by line break contacts = [] persons.each { |person| fields = person.split(/,/) fields.each { |field| field.strip!() } hash = fields_to_hash(fields) contacts.push(hash) } contacts.each { |fields| p(fields[:name], fields[:email]) } end

main()

#for person in persons

puts(person)

#end #for i in 0..(persons.length - 1)

puts persons[i]

#end

Arrays
```ruby
   array << person.split(",")  # << is called a shovel
   array = %w [1,2,3,4,5]
   
   array.pop              #=>= [1,2,3,4]
   array.push(6)          #=> [1,2,3,4,6]  
   array.unshift(5)       #=> [5,1,2,3,4,6] pushes into the first element
   array.shift            #=> [1,2,3,4,6]          pops the first element
   array = (1..10)to_a    #=> [1,2,3,4,5,6,7,8,9,10]
   array = (1...10).to_a  #=> [1,2,3,4,5,6,7,8,9]
array.map do |x|
  x+7
end

array.each_with_index {|v,i| puts v+i }  
array.select {|x| x.is_a? String}  # returns items in the arrays that are Strings
array[1..-2]
array.uniq   #returns unique values

.each performs whatever that's in the codeblock, but does not change the output array

.map performs whatever that's in the codeblock and updates the output array (not the original array)

.each and .map do not modify the original array

.pop, .shift, .unshift, .push modify the original array

ex. :email is a symbol. Symbol is different from variable. The value of a symbol is same as its string name.

API= application programmable interface ( a standardized format for everyone to share) => This is called a Hash rocket CSV = comma separated values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment