Skip to content

Instantly share code, notes, and snippets.

@mathildathompson
Last active August 29, 2015 13:56
Show Gist options
  • Save mathildathompson/9221875 to your computer and use it in GitHub Desktop.
Save mathildathompson/9221875 to your computer and use it in GitHub Desktop.
##RECAP
#Arrays ['literal', 'notation', 555]
#While loops
#If statements
#Functions
#The interpreter reads from top down;
#Compare a sequence of values put them into an array and then can check if it is included in an array;
##HOMEWORK SUBMISSION
#Add your homework under your student name on your local machine;
#git add .
#git commit -m "commit message"
#git push origin master
#create a new pull request with three key points that you want me to look at;
#To pull down new homework from the repo run git pull upstream master;
##HASH
#A hash is a dictionary;
#
vices = {}
vices['Groucho'] = 'cigars'
=>"cigars"
vices['Chico'] = 'cigars'
=>"cigars"
vices['Harpo'] = 'girls'
=>"girls"
vices = {"Groucho"=>"cigars", "Chico"=>"cigars", "Harpo"=>"girls"}
#A hash does not have an order;
#The keys in an array are ordered by index;
vices.keys
=> ["Groucho", "Chico", "Harpo"]
vices.values
=> ["cigars", "cigars", "girls"]
#LITERAL HASH
h = {0 => 'Zero', 1 => 'One', :two => 'Two', 'two' => 2} #This is the old hash rocket notation
h = {0: 'Zero', 1: 'One', 2: 'Two', '}
#The key can be anything such as a string, number, symbol;
#A symbol is most frequently used;
#Everytime you type in a new string, you put it in a new special string object in memory ("Groucho".object_id)
#Wasting memory by constantly creating the Graucho object;
#Ruby has invented a symbol, a symbol is like a string, but all identical symbols hold the same place in memory;
#If you are dealing with simple collections arrays are better, lots of methods such as push and pop;
#Hashes are better for key value pairs;
#(users['Jonathan'][:favorite_numbers] + users['Anil'][:favorite_numbers] + users['Erik'][:favorite_numbers]).uniq.sort
##PRY
#'require' pry at the top of your ruby file;
# Where you want the code to pause in the Ruby environment you can write binding.pry;
##BLOCKS
nums = [1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 39]
i = 0
while i < nums.length
puts "#{nums[i]}"
i+=1
end
while x = nums.shift
puts "#{nums[i]}"
end
#Enumerable means you can do someting to each element in the collection;
#When you have a collection of things you have access to the method 'magic' method each;
kittens = "Buttons:Snowball:Fluffy:Minster".split(':')
kittens.each do |kitten|
puts "The adorable #{kitten}"
end
#kitten within the pipes is an indivudual kitten;
vices = {
:groucho => 'cigars',
:harpo => 'girls',
:chico => 'pranks'
}
vices.each do |brother, vice|
puts "#{brother}, #{vice}"
end
vices.each {|whatsthis| puts "#{whatsthis}"}
12.times do
puts "Beetlejuice"
end
12.times do |iteration|
puts "#{iteration} Beetlejuice!"
end
5.downto(1) do |n|
puts "#{n}"
end
10.upto(23) do |n|
puts "#{n}"
end
(1..100).each do |num|
puts "#{num}"
end
(1..100).to_a
candidates = (1..100).to_a
odds = candidates.select do |i|
i.odd?
end
smalls = candidates.select do |i|
i < 23
end
candidates = (1..10)
#Ruby will let you subtract elements from an array;
(1..10).to_a - [3, 7]
(1..10).to_a - [2, 4, 6 8, 10]
(1..10).to_a + [2, 4, 6, 8, 10]
=> [1, 2, 4, 5, 6, 8, 9, 10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment