Skip to content

Instantly share code, notes, and snippets.

@chhhris
Created June 9, 2013 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chhhris/5745593 to your computer and use it in GitHub Desktop.
Save chhhris/5745593 to your computer and use it in GitHub Desktop.
# write an expression that returns true by using ==
2 == 1 + 1
# write an expression that returns false using ==
2 == 1 + 3
# write an expression that returns true using !=
1 != 2
# write an expression that returns false using !=
2 != 2
# Write an if statement with 3 different branches
# use != in the first branch, == in the second, and > in the third
my_age = 30
if my_age != 30 && my_age < 30
puts "You have your whole life ahead of you."
elsif my_age == 30
puts "Learn to love."
else my_age > 30
puts "With age comes wisdom."
end
# Assign a variable based on the result of an if statement
you_spend = 51
if you_spend > 50
puts "You're eligible for a discount!"
discount = 5
else puts "Keep spending"
end
# Execute code based on the result of an if statement.
# conditionally run puts "Hello Class" if 1 < 2
def execution(x)
if x < 2
puts "Hello Class"
end
end
execution(1)
# Try using an if statement at the end of an expression
number_of_coconut_waters = 10; puts "life is good" if number_of_coconut_waters == 10
# Write an if statement that negates the or operator to return true
if !(!true || !true)
puts "If it aint false and rhymes with blue, it must be... correct."
end
# Write an if statement that uses the and operator to create a false return
if (true && false)
puts "Falsetto"
else puts "Falsetto is not the same as false."
end
# Write a Case Statement that checks if a variable is a vowel
letter = "a"
case letter
when "a", "e", "i", "o", "u", "y"
puts "That letter #{letter} a vowel, baby."
else
puts "Ugh, yet another consonent."
end
# why doesn't this below work?
print "Type in a letter:"
letter = gets.chomp
case letter
when "a", "e", "i", "o", "u"
puts "That letter #{letter} a vowel, baby."
else
puts "Ugh, yet another consonent."
end
# Rewrite that same case statement as an if statement
letter = "a"
if (["a", "e", "i", "o", "u"].include?(letter))
puts "That letter a vowel, baby."
else puts "Ugh, a consonent."
end
# Write a Case statement that has at 4 branches and a default return
dinner = "steamed vegetables"
case dinner
when "pizza"
puts "I'm super jelly you're eating pizza."
when "spaghetti & meatballs"
puts "Damn, enjoy your meal."
when "penne pasta"
puts "One of my favs."
when "steamed vegetables"
puts "At least you're being healthy."
else
puts "You're not eating dinner!?"
end
# Assignment
# Write a while loop that runs exactly 5 times
count = 1
while count <= 5
puts "How many fingers? Let's see, I count " + count.to_s + " fingers."
count += 1
end
# Write a while loop that counts from 1 to 10 and puts all odd numbers
# => you can check if a number is odd by calling the odd? method on it.
# => 1.odd? will return true
count = 1
while count <= 10
if count.odd?
puts count.to_s + " is an odd number."
end
count +=1
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment