Skip to content

Instantly share code, notes, and snippets.

@ryanholm
Last active August 29, 2015 14:02
Show Gist options
  • Save ryanholm/aae5750ab1603e0990ce to your computer and use it in GitHub Desktop.
Save ryanholm/aae5750ab1603e0990ce to your computer and use it in GitHub Desktop.
BLOC Ruby Syntax
#CP1
"hello world"
p "hello world"
p 4
#CP2 Booleans, Symbols, Variables
p true
p false
my_string_variable = "hello world"
p my_string_variable
my_boolean = true
p my_boolean
#CP3 Math Operations
p 1 + 2
p 5 - 1
p 6 / 2
p 4 * 3
p 2 ** 3
x = 10
p x
x = x + 5
p x
x += 5
p x
p 4 > 5
p 3 < 6
p 5 > 5
p 4 == 5
p 3 == 3
p (3 + 4) * 7
p 3 + 4 * 7
sum = 3 + 4
p sum * 7
wins = 11
losses = 5
p wins > losses
p wins == losses
hello = "hello"
p hello * 3
p hello + "world"
p "hello #{3}"
num = 5
p "hello #{num}"
p "3 + 4 = #{3 + 4}"
num1 = 3
num2 = 4
p "3 + 4 = #{num1 + num2}"
num1 = 3
num2 = 4
p "#{num1} + #{num2} = #{num1 + num2}"
p = 10_000
r = 0.05
t = 5
n = 12
a = p * (1 + r/n) ** (n*t)
p "After #{t} years I'll have #{a} dollars!"
#CP4 Nil
p "Bloc"[7]
p "Bloc"[2]
Returns nil or "o"
#CP5 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(1,2)
p add(4,5)
p add(7,7)
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
def link(address,text)
"<a href='#{address}'>#{text}</a>"
end
p link("http://www.google.com", "Google")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment