Skip to content

Instantly share code, notes, and snippets.

@JoseJRVazquez
Created April 3, 2014 03:55
Show Gist options
  • Save JoseJRVazquez/9948051 to your computer and use it in GitHub Desktop.
Save JoseJRVazquez/9948051 to your computer and use it in GitHub Desktop.
Ruby Notes: First Checkpoints
The Difference between p and PUTS = Puts results in the value output as a string, whereas p simply outputs the true value of the object
Variables must start with lower case letter:
my_string_variable = "hello world"
p my_string_variable
will print out
hello world
when running a Boolean
my_boolean = true
p my_boolean
will output
true
You can use commans to define more than one item : a, b = 1, 2
would assign a=1 and b=2
Symbols are like strings, only with less functionality. A Symbol is designated with a colon (:) to the left of a word. Symbols can contain any alpha-numeric character, but they must start with a letter
For example
p :lowdown123 (starts with letter after colon
using underscores such as this my_variable is called SNAKECASE
using various uppercases such as myOldApple is called CAMELCASE
Does normal math operations, ** = exponents (example: 2 ** 3 = 8)
You can assign math variables as numbers: for example
x = 20
p x
x = x + 5
p x will output 25
when comparing to numbers you can use > or < for a boolean return of True False
but for equals, you have to use double equals ( 5 == 5)
You can use variables in math operations such as
jose = 37
melissa = 38
melissa > jose = true
Personal note: If I wanted to get a users name inserted into a greeting I would od the following:
set the variable (name = "Jose")
Then I could print out a greeting with that name such as
p "hello " + name which would result in hello Jose
DRY = Dont repeat yourself
for example
num1 = 3
num2 = 3
p "#{num1} + #{num2} = #{num1 + num2}"
is easier than
18003384752 - tg.org for student loans for judy
Cool formula
# principal amount
p = 132_000
# annual rate of interest
r = 0.0541
# number of years
t = 10
# number of times it is compounded
n = 12
# amount accumulated
a = p * (1 + r/n) ** (n*t)
p "After #{t} years, Law school will have cost you an unreasonable #{a} dollars "
this would be great for the site: A calculator for parents to see how much it will cost them
the Pound# Sign is a comment line
the underscore _ replaces other items you cant use in code for readbility, such as a comma
NIL finanlly im going to get this
Square brackets ([]) are used in Ruby to reference locations. so if I ask p "Jose"[4], i wil get "s" for the output. It is trying to find the 4 character in the string which includes the quotes. Now if I tried "Jose"[7] I would get a nil because it doesnt exist.
Methods: Rather than reapat lines of code, you can call a method you defined instead. By calling the method you can make the line reperform its task.
```
CALL = is to execute the line
Define = defines the method itself, to compose or write a method.
```
to define a method, you must do the following
```
def hello
"Hello world"
end```
it must start with def to define it, and end with end to end it
In the above forumla, the word hello is the method name. If I run it after adding ```p hello``` i will get out ```Hello World```
def hello2
a = 1
b = 2
a + b
end
this one is cool, because it will excute the last line as the output when you ```p hello2```
num1 = 3
num2 = 3
p "3 + 4 = #{num1 + num2}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment