Last active
August 29, 2015 14:25
-
-
Save coopermayne/e9d6d393f48e3b03cc21 to your computer and use it in GitHub Desktop.
NOTES
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# NOTES | |
[TOC] | |
##STRINGS | |
```ruby | |
"strings look like this." | |
``` | |
###index reference | |
```ruby | |
"biilliio"[3] | |
"biilliio"[-2] | |
``` | |
###index reference | |
```ruby | |
"toaster"[3] #=> "s" | |
"zebra"[-2] #=> "r" | |
``` | |
###length | |
```ruby | |
"biilliio".length #=> "8" | |
``` | |
###concatenation | |
```ruby | |
"hello " + "world" #=> "hello world" | |
``` | |
###conversion | |
```ruby | |
"1001".to_i #=> 1001 | |
"1001".to_f #=> 1001.0 | |
``` | |
###split | |
**split** will turn a sentence into an array of words. this can be very useful | |
```ruby | |
sentence = "i am about to be split" | |
sentence.split #=> ["a", "senetence", "written", "out"] | |
``` | |
you can save this array to another variable so you can use it.... | |
```ruby | |
sentence = "run to the store for me, please" | |
split_sentence = sentence.split | |
split_sentence[0] #=> "run" | |
split_sentence[3] #=> "store" | |
``` | |
##INTEGERS/FLOATS | |
###math | |
```ruby | |
1+1 #=> 2 | |
1-1 #=> 0 | |
2*2 #=> 4 | |
6/2 #=> 3 | |
``` | |
###conversion | |
```ruby | |
1001.to_s #=> "1001" | |
1001.to_f #=> 1001.0 | |
``` | |
###floats | |
floats are just decimal numbers... if you need exact answer to a mathematical expression you'll use floats. | |
```ruby | |
1/3 #=> "0" | |
1.0/3 #=> 0.3333333333333333 | |
``` | |
to go back to integer - you use *floor*, *ceil* and *round*. | |
```ruby | |
fl = 0.333 | |
fl.floor #=> 0 | |
fl.ceil #=> 1 | |
fl.round #=> 0 | |
``` | |
##INPUT/OUTPUT | |
###ouput | |
puts is the command you use to print things to the screen. | |
```ruby | |
puts "some string" | |
``` | |
###input | |
```ruby | |
gets | |
``` | |
short for *get string*. **gets** will open a prompt for user input. but to do anything with their response we need to save it to a variable like this: | |
```ruby | |
some_variable = gets | |
``` | |
##ARRAYS | |
an array is just a collection of stuff. the computer doesn't care what kind of stuff you put into the array. it treats it all the same. you can put **strings** in there... **numbers**... even **another array**! | |
```ruby | |
ex1 = [1,2,3,4] | |
ex2 = ["cooper", "sam", "miles", "noah"] | |
ex3 = [ [1,2], "sam", 3, [4,5,6] ] | |
``` | |
###length | |
```ruby | |
ex1 = [1,2,3,4] | |
ex2 = ["cooper", "sam", "miles", "noah"] | |
ex1.length #=>4 | |
ex2.length #=>4 | |
``` | |
###index reference | |
```ruby | |
ex1 = [1,2,3,4] | |
ex2 = ["cooper", "sam", "miles", "noah"] | |
ex1[2] #=> 3 | |
ex2[0] #=> "cooper" | |
ex2[-1] #=> "noah" | |
ex2[-2] #=> "miles" | |
``` | |
using negative numbers isn't necessary. it is just a shortcut. you can also use the length method like this: | |
```ruby | |
ex1 = [1,2,3,4] | |
last_index = ex1.length - 1 | |
ex1[last_index] #=> 4 | |
``` | |
###append | |
```ruby | |
a = ["i'm", "in", "an", "array"] | |
a << "array" | |
a #=> ["i'm", "in", "an", "array"] | |
a2 = [1,2,3] | |
a2 << 4 << 5 | |
a2 #=> [1,2,3,4,5] | |
``` | |
###delete_at | |
this is an important method. when you want to remove an item from a collection you need to reference it by its index. see example below. | |
```ruby | |
ex = [1,2,3,4,5] | |
ex.delete_at(2) | |
ex #=> [1,2,4,5] | |
``` | |
and here's a little trick. you can save what you delete to a variable like this: | |
```ruby | |
ex = [1,2,3,4,5] | |
saved_item = ex.delete_at(2) | |
ex #=> [1,2,4,5] | |
saved_item #=> 3 | |
``` | |
##CONDITIONALS | |
###booleans - true/false | |
a **boolean** a data type having two possible values: “true” or “false.” | |
when we are checking for equality we use two "=" signs. like this: | |
```ruby | |
test_string = "biilliio" | |
test_string == "biilliio" #=> true | |
test_string == "bilio" #=> false | |
test_num = 3 | |
test_num == 3 #=> true | |
test_num == 4 #=> false | |
``` | |
###the big "if" | |
conditionals are written like this: | |
```ruby | |
if boolean | |
#if boolean is true, this code will run | |
else | |
#if boolean is false, this code will run | |
end | |
``` | |
here are some real examples | |
```ruby | |
if 3==3 | |
result = "three is equal to three" | |
else | |
result = "three is not equal to three" | |
end | |
result #=> "three is equal to three" | |
name = "noah" | |
if name == "cooper" | |
result = "we have the same name!" | |
else | |
result = "we have different names" | |
end | |
result #=> "we have different names" | |
``` | |
##LOOPS | |
###each | |
the **each** method is used on arrays. it allows you to apply the same code many times while only writing it once. so if you want to double 5 numbers or 10000 numbers you wouldn't have to change you code. | |
```ruby | |
numbers = [1,2,3,4] | |
numbers_doubled = [] | |
numbers.each do |number| | |
numbers_doubled << number * 2 | |
end | |
numbers_doubled #=> [1,4,6,8] | |
``` | |
###each_with_index | |
very similar to **each**. but his one gives you access to the index as well. see example. | |
```ruby | |
items = ["gun", "knife", "pasta"] | |
items.each_with_index do |item, index| | |
puts index.to_s + ": " + item | |
end | |
#=> "0: gun" | |
#=> "1: knife" | |
#=> "2: pasta" | |
``` | |
experiment with this one to make sure you understand how it works | |
# NOTES | |
[TOC] | |
##STRINGS | |
```ruby | |
"strings look like this." | |
``` | |
###index reference | |
```ruby | |
"biilliio"[3] | |
"biilliio"[-2] | |
``` | |
###index reference | |
```ruby | |
"toaster"[3] #=> "s" | |
"zebra"[-2] #=> "r" | |
``` | |
###length | |
```ruby | |
"biilliio".length #=> "8" | |
``` | |
###concatenation | |
```ruby | |
"hello " + "world" #=> "hello world" | |
``` | |
###conversion | |
```ruby | |
"1001".to_i #=> 1001 | |
"1001".to_f #=> 1001.0 | |
``` | |
##INTEGERS | |
###math | |
```ruby | |
1+1 #=> 2 | |
1-1 #=> 0 | |
2*2 #=> 4 | |
6/2 #=> 3 | |
``` | |
###conversion | |
```ruby | |
1001.to_s #=> "1001" | |
1001.to_f #=> 1001.0 | |
``` | |
##INPUT/OUTPUT | |
###ouput | |
puts is the command you use to print things to the screen. | |
```ruby | |
puts "some string" | |
``` | |
###input | |
```ruby | |
gets | |
``` | |
short for *get string*. **gets** will open a prompt for user input. but to do anything with their response we need to save it to a variable like this: | |
```ruby | |
some_variable = gets | |
``` | |
##ARRAYS | |
an array is just a collection of stuff. the computer doesn't care what kind of stuff you put into the array. it treats it all the same. you can put **strings** in there... **numbers**... even **another array**! | |
```ruby | |
ex1 = [1,2,3,4] | |
ex2 = ["cooper", "sam", "miles", "noah"] | |
ex3 = [ [1,2], "sam", 3, [4,5,6] ] | |
``` | |
###length | |
```ruby | |
ex1 = [1,2,3,4] | |
ex2 = ["cooper", "sam", "miles", "noah"] | |
ex1.length #=>4 | |
ex2.length #=>4 | |
``` | |
###index reference | |
```ruby | |
ex1 = [1,2,3,4] | |
ex2 = ["cooper", "sam", "miles", "noah"] | |
ex1[2] #=> 3 | |
ex2[0] #=> "cooper" | |
ex2[-1] #=> "noah" | |
ex2[-2] #=> "miles" | |
``` | |
using negative numbers isn't necessary. it is just a shortcut. you can also use the length method like this: | |
```ruby | |
ex1 = [1,2,3,4] | |
last_index = ex1.length - 1 | |
ex1[last_index] #=> 4 | |
``` | |
###append | |
```ruby | |
a = ["i'm", "in", "an"] | |
a << "array" | |
a #=> ["i'm", "in", "an", "array"] | |
a2 = [1,2,3] | |
a2 << 4 << 5 | |
a2 #=> [1,2,3,4,5] | |
``` | |
###delete_at | |
this is an important method. when you want to remove an item from a collection you need to reference it by its index. see example below. | |
```ruby | |
ex = [1,2,3,4,5] | |
ex.delete_at(2) | |
ex #=> [1,2,4,5] | |
``` | |
and here's a little trick. you can save what you delete to a variable like this: | |
```ruby | |
ex = [1,2,3,4,5] | |
saved_item = ex.delete_at(2) | |
ex #=> [1,2,4,5] | |
saved_item #=> 3 | |
``` | |
##CONDITIONALS | |
###booleans - true/false | |
a **boolean** a data type having two possible values: “true” or “false.” | |
when we are checking for equality we use two "=" signs. like this: | |
```ruby | |
test_string = "biilliio" | |
test_string == "biilliio" #=> true | |
test_string == "bilio" #=> false | |
test_num = 3 | |
test_num == 3 #=> true | |
test_num == 4 #=> false | |
``` | |
###the big "if" | |
conditionals are written like this: | |
```ruby | |
if boolean | |
#if boolean is true, this code will run | |
else | |
#if boolean is false, this code will run | |
end | |
``` | |
here are some real examples | |
```ruby | |
if 3==3 | |
result = "three is equal to three" | |
else | |
result = "three is not equal to three" | |
end | |
result #=> "three is equal to three" | |
name = "noah" | |
if name == "cooper" | |
result = "we have the same name!" | |
else | |
result = "we have different names" | |
end | |
result #=> "we have different names" | |
``` | |
##LOOPS | |
###each | |
the **each** method is used on arrays. it allows you to apply the same code many times while only writing it once. so if you want to double 5 numbers or 10000 numbers you wouldn't have to change you code. | |
```ruby | |
numbers = [1,2,3,4] | |
numbers_doubled = [] | |
numbers.each do |number| | |
numbers_doubled << number * 2 | |
end | |
numbers_doubled #=> [1,4,6,8] | |
``` | |
###each_with_index | |
the only difference between this and the normal **each** is that you have access to the index inside of the loop. see example. | |
```ruby | |
numbers = [1,2,3,4] | |
numbers_doubled = [] | |
indices = [] | |
numbers.each do |number, index| | |
numbers_doubled << number * 2 | |
indices << index | |
end | |
numbers_doubled #=> [1,4,6,8] | |
indices #=> [0,1,2,3] | |
``` | |
###times | |
```ruby | |
test_array = [] | |
indices = [] | |
5.times do |index| | |
indices << index | |
test_array << 42 | |
end | |
index #=> [0, 1, 2, 3, 4, 5] | |
test_array #=> [42, 42, 42, 42, 42] | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment