Last active
December 30, 2015 10:49
-
-
Save randalmaile/7818761 to your computer and use it in GitHub Desktop.
Ruby Syntax ckpt
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
1. Interpolated strings must be enclosed in double quotes. The Ruby interpreter will not search for interpolated values in a string if single quotes are used: | |
``` | |
p 'hello #{3}' | |
``` | |
2. '#' is used as a comment in Ruby | |
3. '_' is used for readablility in numbers | |
4. There's a special object in Ruby named nil. Nil is a representation of nothingness. Nil is important in programming because it helps programmers determine the validity of an operation. | |
5. "In Ruby, the last line of code in the method is automatically returned by the method." - **That's awesome - so simple!!!** | |
6. Arguments don't need to be the same type. For example, the method below takes three different types of arguments. (String, Integer, Float) | |
7. print vs put: both methods display the arguments passed to them as STRING, puts also inserts a carriage return at the end of the output | |
8. Ruby has another shortcut, which allows us to create an Array from a list of Strings: the %w( ) syntax. Using this shortcut saves us from having to type a lot of double- quote characters: | |
irb> available_colors = %w( red green blue black ) => ["red", "green", "blue", "black"] | |
9. |
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
"simple math" | |
3 | |
4 | |
3 | |
12 | |
"exponents" | |
8 | |
"operations run on variables" | |
10 | |
15 | |
"shorthand syntax" | |
10 | |
15 | |
"math op return bool" | |
false | |
true | |
false | |
true | |
"comparison operator" | |
false | |
true | |
"PRACTICE" | |
"order of operations" | |
49 | |
31 | |
"combine operations using variables" | |
49 | |
"combine variables w/ comp oper" | |
true | |
false | |
"operations on string" | |
"hellohellohello" | |
"what if I want to add a space?" | |
"hello hello hello " | |
"wow, that's cool" | |
"helloworld" | |
"adding string to number" | |
"p 'hello' + 3 throws an error - that's interesting - JS will do it..." | |
"String Interpolation" | |
"hello 3" | |
"hello 5" | |
"more fun w/ $ interpolation" | |
"3 + 4 = 7" | |
"3 + 4 = 7" | |
"staying DRY" | |
"5 + 333333 = 333338" | |
"it better because if I cange vars num1 and/or num2, I it's already taken care of in the p statement" | |
"After 5 years I'll have 12833.586785035119 dollars!" | |
nil | |
"o" | |
nil | |
"Hello world" | |
3 | |
4444 | |
"var1 is 300" | |
"var2 is 14" | |
"300 + 14 = 314" | |
"After 12 years Bob will have 735.8417318419806 dollars!" | |
"After 12 years Joe will have 4983.988855879855 dollars!" | |
"<a href='http://google.com'>Google</a>" |
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
1. What really is nil - why is it important - for instance, tut says: "...important because it helps programmers determine the validity of an operation." Can you give me some examples where this is used in a program? |
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
# Math Operations | |
p "simple math" | |
p 1+2 | |
p 5-1 | |
p 6/2 | |
p 4*3 | |
p "exponents" | |
p 2**3 | |
p "operations run on variables" | |
x=10 | |
p x | |
x = x+5 | |
p x | |
p "shorthand syntax" | |
x=10 | |
p x | |
x += 5 | |
p x | |
p "math op return bool" | |
p 4>5 | |
p 3<6 | |
p 5>5 | |
p 5>=5 #should return true | |
p "comparison operator" | |
p 4==5 | |
p 3==3 | |
#p 3=3 # just curious what this returns... | |
p "PRACTICE" | |
p "order of operations" | |
p (3+4) * 7 | |
p 3+4*7 | |
p "combine operations using variables" | |
sum = 3+4 | |
p sum * 7 | |
p "combine variables w/ comp oper" | |
wins = 11 | |
losses = 5 | |
p wins > losses | |
p wins == losses | |
p "operations on string" | |
hello = "hello" | |
p hello * 3 | |
p "what if I want to add a space?" | |
p (hello + " ")*3 | |
p "wow, that's cool" | |
p hello + "world" | |
p "adding string to number" | |
p "p 'hello' + 3 throws an error - that's interesting - JS will do it..." | |
p "String Interpolation" | |
p "hello #{3}" | |
num = 5 | |
p "hello #{num}" | |
p "more fun w/ $ interpolation" | |
p "3 + 4 = #{3+4}" | |
num1 = 3 | |
num2 = 4 | |
p "3 + 4 = #{num1+num2}" | |
p "staying DRY" | |
num1 = 5 | |
num2 = 333333 | |
p "#{num1} + #{num2} = #{num1 +num2}" | |
p "it better because if I cange vars num1 and/or num2, I it's already taken care of in the p statement" | |
# principal amount | |
p = 10_000 | |
# annual rate of interest | |
r = 0.05 | |
# number of years | |
t = 5 | |
# number of times it is compounded | |
n = 12 | |
# amount accumulated | |
a = p * (1 + r/n) ** (n*t) | |
p "After #{t} years I'll have #{a} dollars!" | |
# Nil | |
p "Bloc"[7] | |
p "Bloc"[2] | |
nilly = nil | |
p nilly | |
# Methods | |
def hello | |
"Hello world" | |
end | |
p hello | |
def hello2 | |
a = 1 | |
b = 2 | |
a + b | |
end | |
p hello2 | |
def add(a, b) | |
a+b | |
end | |
p add(2222, 2222) | |
# passing variables as args | |
num1 = 10 *30 | |
num2 = ((3+4) *2) | |
num3 = add(num1, num2) | |
p "First number is #{num1}" | |
p "Second number is #{num2}" | |
p "Adding the 2 numbers: #{num1} + #{num2} = #{num3}" | |
# p = principal amount | |
# r = annual rate of interest | |
# t = number of years | |
# n = number of times it is compounded | |
def compound_interest(name, p, r, t, n) | |
a = p * (1 + r/n) ** (n*t) | |
"After #{n} years #{name} will have #{a} dollars!" | |
end | |
p compound_interest("Bob", 100, 0.05, 40, 12) | |
p compound_interest("Joe", 250, 0.06, 50, 12) | |
# Practice - output a hyperlink: | |
def link(address, text) | |
"<a href='#{address}'>#{text}</a>" | |
end | |
p link("http://google.com", "Google") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment