Skip to content

Instantly share code, notes, and snippets.

@worace
Created March 22, 2016 17:33
Show Gist options
  • Save worace/fa668ad4c0107262b816 to your computer and use it in GitHub Desktop.
Save worace/fa668ad4c0107262b816 to your computer and use it in GitHub Desktop.
age = 5
wishes = (age * "happy " + "Birthday!").capitalize
puts wishes
puts "HI GUYS"
puts "10 times 10 is #{10 * 10}"
num = 50 * 5 # assignment operator evaluate right side
puts "NUMMMMM: #{num}"
# objects
# methods
# string = "pizza"
# string[0]
# string.[](0)
# Keywords
# def
# if
# else
# end
# class
# "Truthiness" -- any object besides nil and false
# is treated as "truthy"
# nil and false are treated as "Falsey"
# want to choose between two things
# one side or the other but not both
condition = false
if condition
puts "CONDITION IS TRU-thy!"
# body of one branch
else
# body of the other branch
puts "CONDITION IS FALSE-y"
end
# branch between multiple choices
condition_one = false
condition_two = true
condition_three = true
# Choose first branch for which the condition
# holds true
if condition_one
puts "FIRST CONDITION"
elsif condition_two
puts "SECOND CONDITION"
elsif condition_three
puts "THIRD CONDITION"
else
puts "LAST CONDITION"
end
condition = false
if condition
puts "CONDITION IS TRUE"
else
nil
end
condition_one = false
condition_two = true
condition_three = true
# Choose first branch for which the condition
# holds true
if condition_one
puts "FIRST CONDITION"
elsif condition_two
puts "SECOND CONDITION"
elsif condition_three
puts "THIRD CONDITION"
else
"Sorry, input not understood."
end
# Negating conditions
condition = true
unless condition
"do this only if the condition is false-y"
end
if !condition
"do this if the condition is false-y"
end
# Flow Control tools
# Do a thing multiple times
# until : while :: unless : if
# while
# until
# for
# loop
# times method on integers
# while true
# puts "repeat this as long as the condition is truth-y"
# end
loop do
puts "repeat this forever"
end
some_variable = counter < 2
# do some series of steps
# X number of times
counter = 0
# if the condition is false-y, your loop
# won't even execute once
while counter < 2
puts "my message! #{counter}"
counter += 1 # only increment the counter under certain conditions, etc
end
until (counter < 2)
puts "my message! #{counter}"
# do these things
end
# "times" method
# -- call this on any Number
5.times do
# want to know which iteration i'm
puts "Doing stuff 5 times"
end
6.times do |i| # <-- block variable
# within this block can use "i"
# to refer to the number of the current iteration
puts i
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment