Skip to content

Instantly share code, notes, and snippets.

@screamingmunch
Last active December 18, 2015 15:49
Show Gist options
  • Save screamingmunch/5807071 to your computer and use it in GitHub Desktop.
Save screamingmunch/5807071 to your computer and use it in GitHub Desktop.
GA Lecture Notes

tl;dr: Ruby Arrays,

Objectives:MVC & Testing , Arrays & Functions ,Networking & HTTP

Ruby "Strings" vs. [Arrays]

    array: an Object with ordered storage container
```Ruby names = ["Travis", "Megan", "Tod", "Bryan"] #literal array- what you see is what you get, also json (JS object notation) #json simply means a literal array that can be copied & pasted into other programs require "json" names.methods().sort() # gives you all the methods available to your array & json names.to_json #=> "[\"Travis\", \"Megan\", \"Tod\", \"Bryan\"]" ``` ```bash j@ubuntu~$ ~/.rvm/ j@ubuntu~$ find ~/.rvm/ j@ubuntu~$ find ~/.rvm/ | grep 'array' ```
More on Strings & Arrays
```Ruby var = "hi there" var[2] #=> ' ' var[-1] #=> 'e' #last character on your string var.inspect #=> "\"hi there\"" # \\ escapes the string var.class #=> String var.class.ancestors #=> [String, Comparable, Object, Kernel, BasicObject] ``` Numbers: ```ruby num1 = 42 num2 = 42.0 num1.class = Fixnum num2.class = Float **because of this, anything that deals with money, operate the math by multiplying 100 then divide by 100 when you're done with it PEMDAS - Parens, Exponents, Multiplication, Division, Addition, Subtraction ex. 2+((3*4) / (2**3)) ``` Truth/False: In Ruby, only "nil" and "false" are falsey. This is not the case in other languages. ```ruby if 0 puts "this is true" #=> this is true if false puts "false" #=> false

*truthy: "Strings", true, " ", everything else *falsey: nil, false

true || true #=> true true || false #=> false false|| false #=> false


In ruby, it is good practice to default something to nil then assign something value to it later on.


bash$ gem install pry  

  => This goes to rubygems.org online to download the "pry" gem


Book: Ruby under a microscope

<strong>Naming convention:</strong>

Use snake_case for methods and variables.

Use CamelCase for classes and modules. (Keep acronyms like HTTP, RFC, XML uppercase.)

Use SCREAMING_SNAKE_CASE for other constants.

The names of predicate methods (methods that return a boolean value) should end in a question mark. (i.e. Array#empty?).

The names of potentially "dangerous" methods (i.e. methods that modify self or the arguments, exit!, etc.) should end with an exclamation mark. Bang methods should only exist if a non-bang method exists. (More on this).



<h4>Homework Review:</h4>    
```Ruby
#!/usr/bin/env ruby

# 1. create a literal array of all the names in class
# 2. Accept '-g' or 'n'
# 3. Accept a number for either
# 4. when you get '-g x', split the array into 'x' groups and print them
# 5. when you get '-n x', print out 'x' names

# Bonus 1- Store/ retrieve the array in a json file

# Bonus 2- Keep track of selected names

names = %w[Josh Kevin Travis Dave Kristine Donielle Lauren
  	    Megan Xiao Sharif Theo Ricky Justin Ian Ryan Brian  
		                               Alex Elen Lior Tod Karl]
puts "Class size is #{names.length}"

	method = ARGV[0]
	num    = ARGV[1].to_i

	if '-g' == method
		puts
		puts "Dividing class into #{names.length/num} groups of #{num}'s:"
		puts
		p names.shuffle.each_slice(num).to_a
		#p xx is equivalent to puts xx.inspect (prints the value of 
                #inspect instead of to_s, suitable for debugging)

	elsif '-n' == method
		puts "selecting #{num} random number of peeps:"
		p names.sample(num)

	else
		puts "Error. Check your input."
	end	

Cheap Tricks:

*Xmodmap - to change your keyboard to Dvorak vs. Querty

*While in Terminal, to get to beginning of line: Ctrl + a; to get to end of line: Ctrl + e

*for Mac- if you're running into issues opening a non-mac program, press Appl Ctrl then mouse click on the icon to open that way.

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