Welcome to my cheat sheet! I'm going to edit this document as I progress through dev bootcamp, documenting those concepts that I find are essential to know.
Ruby BASICS
Material coming soon!Ruby Enumerables
The ruby enumberable methods are used to iterate through each component of an object:- each object of an array
- each key-value pair of a hash
- each key of a hash
- each value of a hash
- each number in a range
- #each
- performs a specific block of code for each object in an array, or for each key-value pair in a hashnums = [1,2,3,4,5]
nums.each {|num| puts num*2}2
4
6
8
10
- #reverse_each
- the same as #each, except that iterates through each object backwardsnums = [1,2,3,4,5]
nums.reverse_each {|num| puts num*2}10
8
6
4
2
- #map
- creates a new array, replacing each value of the current array with the output of the code block passed at every iterationnums = [1,2,3,4,5]
nums.map {|num| num*3}[3,6,9,12,15] - #collect
- same as the #map method - #select
- creates a new array with the values from the original array that satisfy a condition imposed by the blocknums = [1,2,3,4,5]
nums.select {|num| num % 2 == 0}[2,5] - #inject
- works like the #each method, except it also defines a variable whose value is set to the output of every iteration (the starting point of this variable must be defined)
nums = [1,2,3,4,5]
nums.inject(0) {|sum, num| sum += num}15 - #cycle
- works like the #each method, except it doesn't stop at the last iteration. It starts from the beginning again and cycles through the code an unlimited amount of times, unless a specific cycle count is defined, or if there is a specific breaking pointnums = [1,2,3,4,5]
i = 0
nums.cycle {|num| puts num; i += 1; break if i == 15}1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
nums = [1,2,3,4,5]
nums.cycle(3) {|num| puts num}1
2
3
4
5
1
2
3
4
5
1
2
3
4
5
- #include?
- returns true or false if the array it is called on includes a specific value
nums = [1,2,3,4,5]
nums.include?(3)
nums.include?(6)true
false
Ruby Hashes, Arrays, Strings, Integers
Material coming soon!JavaScript Objects
Material coming soon!JavaScript prototypes
Material coming soon!Looping in Ruby and Javascript (comparisons)
Material coming soon!HTML
Material coming soon!CSS
Material coming soon!GIT
Material coming soon!SQL
Material coming soon!Copyright: Gary Hammell 2014