Skip to content

Instantly share code, notes, and snippets.

@elenawalom
Forked from ianyang/GA Day2-3.md
Created June 20, 2013 17:03
Show Gist options
  • Save elenawalom/5824559 to your computer and use it in GitHub Desktop.
Save elenawalom/5824559 to your computer and use it in GitHub Desktop.

General

  • method_name() to run a method with no parameters
  • .split splits a string into an array based on spaces
    • .split() splits a string into an array with the value inside the parens
    • "\n" is class block
  • Make sure the remember that an array starts with 0, and so the top number needs to be subtracted by 1
  • .gsub(/thing/, "substitute") substitutes "thing" with "substitute"
    • if "thing" is a parenthesis, use "" instead of //
  • {} is used for a code block (as opposed to do and end), but also for hash. It might be worth using do and end for a code block
  • refactor means cleaning up code

Arrays

  • << element to add things to an array (modify array)
  • .push(element) to add things (modify array)
  • .pop removes the last element (modify array)
  • .unshift(element) to add element to the beginning
  • .shift returns the first element that has been popped off
  • .map {} performs things in an array but shows the output in the array, where as each {} shows the output in individual array elements
  • .each_with_index {} shows each of the element with the position (e.g.0, 1, 2, 3)
  • .index(element) to find the position of the element in the array. If there are multiple elements of the same values, it will show the position of the first one.
  • .select {} eliminates something in an array
  • array[x..y] is a range and will display the elements between x and y positions

Range

  • x..y range from x to y
  • x…y range from x to y-1

Hash

  • Hash literal is name = {}
  • Hash is how the internet organizes information: key value pairs

name = { key: value, key2: value2. }

older way:

name = { :key => value, :key2 => value2, }

Symbol

  • A symbol has a value that equals its name
  • Format of :name

Loop

for i in 1..100 puts i end

Block

  • Block is like a function with pipes. Pipes perform the same function as a paren in a method

a.each do |x| puts x end

Boolean operator has a question mark at the end

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