Skip to content

Instantly share code, notes, and snippets.

@jbsulli
Last active August 22, 2017 04:33
Show Gist options
  • Save jbsulli/62c53bc7426803429a977d183bbc8614 to your computer and use it in GitHub Desktop.
Save jbsulli/62c53bc7426803429a977d183bbc8614 to your computer and use it in GitHub Desktop.
App Academy JumpStart Prep Notes

Primatives

  • string
    • .chars.count: block receives each char and method will return the number of truthy results
    • .split
    • .each_char: block called for each char with the current char as the argument
    • .with_index: when used with .each_char, will add a second argument, index, passed to the block
  • number
    • .even?/.odd?
    • .abs
    • .floor, .ceil, .round
    • .lcm (8.lcm(12) => 24)
    • .gcd (8.gcd(12) => 4)
    • .to_i, .to_f, .to_s
  • boolean
  • nil
  • symbols
    • :+
    • :-
    • :*
    • :/

Console

  • puts: stringifies values and prints them to console

Error

  1. Message
  2. Stack trace (ex: (repl):1:in '<main>')

Variables

var = evaluated_expression Use snake_case!

Comments

# Comment or

=begin
Look!
I'm a comment
=end

Operators

+, -, *, /, **, % Use PE[MD][AS] 2 + 2.0 => 4.0 but 2 + 2 => 4

Methods

reciever.method(argument) => return value

def method_name(arg1, arg2)
  arg1 + arg2 # implicit return
end
def has_unreachable(value)
  return value
  value = value + 1 # unreachable code
end

Arrays

  • heterogeneous arrays: different data types contained in the array

access:

arr = [  1,  2,  3,  4,  5]
         0   1   2   3   4
        -5  -4  -3  -2  -1
        
0..2  # inclusive 0, 1, 2
0...2 # exclusive 0, 1
arr[0..2] => [1, 2, 3]
arr[3..-1] => [4, 5]
arr.[](0) => 1
arr[5] => nil
("a".."c").to_a => ["a", "b", "c"]
(0..5).to_a = [0, 1, 2, 3, 4, 5]

methods:

  • .first, .last
  • .length
  • .push, .pop, .shift, .unshift
  • .join
  • .sort, .sort!
  • .reverse, .reverse!
  • .include?
  • .each
  • .each_index
  • .all?: check receiver values to see if all values pass a test
  • .none?: check receiver values to make sure none pass the test
  • .any?: check receiver values to see if any pass the test
  • .map, .map!: create a new array based on the return value of the passed block
  • .count: returns the length of the array, or the number of matching values, or the number of elements that pass a block test
  • .select, .select!: return new array with all elements that passed the block test
  • .reject, .reject!: return new array with all elements that did NOT pass the block test
  • .sort_by: block should return a number that represents how each element should be sorted (least to greatest)
  • .each_with_index: block has to arguments, value and index, for each element
  • .with_index: can be used to add an index argument to the block when used with .map
  • .reduce, .inject: when used with a symbol (ex: :+) the elements will be combined using that symbol (so all elements would be added together). When used with a block, the block will receive an accumulator and element argument. The first accumulator can be passed in as a method parameter; otherwise, it uses the first value in the receiver.

assignment/concatination

arr[3..-1] = 6, 7 => [1, 2, 3, 6, 7]
arr[7] = 8 => [1, 2, 3, 4, 5, nil, nil, 8]
arr.concat([6, 7]) => [1, 2, 3, 4, 5, 6, 7]
arr = arr + [6, 7] => [1, 2, 3, 4, 5, 6, 7]
arr << [6, 7] => [1, 2, 3, 4, 5, [6, 7]]

comparison

["cat"] == ["dog"] => false
["cat"] >= ["dog"] => throws error

Strings

access/assignment/concatination:

aa = "App Academy"
aa[3] => " "
aa[0..2] => "App"
aa[0..3] = "Best " => "BestAcademy"
"echo" * 3 => "echoechoecho"
aa << "!" => "App Academy!"
aa.concat("!")

methods

  • .chars
  • .length
  • .reverse, .reverse!
  • .include?
  • .upcase, .downcase, .capitalize
  • .split
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment