Created
June 9, 2013 22:05
-
-
Save surrealdetective/5745444 to your computer and use it in GitHub Desktop.
using logic in ruby
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
#Assignment.rb | |
#https://gist.github.com/aviflombaum/fd375100f4410dbbf184 | |
# Write an expression that returns true by using == | |
8*8 ==64 | |
# Write an expression that returns false using == | |
rand(10) == 11 | |
# Write an expression that returns true using != Write an expression that returns false using != | |
1 != 2 #true | |
1 != 1 #false | |
# Write an if statement with 3 different branches use != in the first branch, == in the second, and > in the third | |
age = rand(78) | |
if age != 25 | |
puts "We are not the same age!" | |
elsif age == 25 | |
puts "We are the same age!" | |
elsif age > 25 | |
puts "You are older than me." | |
end | |
# Assign a variable based on the result of an if statement | |
x = 25 if rand(10) > 5 | |
# Execute code based on the result of an if statement. conditionally run puts "Hello Class" if 1 < 2 | |
if 1 < 2 | |
puts "Hello Class" | |
end | |
# Try using an if statement at the end of an expression | |
hilarious = if 100*1 > 99 then 'true' end | |
# Write an if statement that negates the or operator to return true | |
#Write an if statement that uses the and operator to create a false return | |
if !(false||false) | |
puts "I negated the or operator, but returned true." | |
end | |
if 8 > 9 and 9 > 8 | |
puts "unbelievable!" | |
else | |
puts "impossible!" | |
end | |
# Write a Case Statement that checks if a variable is a vowel | |
#Rewrite that same case statement as an if statement | |
#Write a Case statement that has at 4 branches and a default return | |
var = gets.chomp | |
case var | |
when "a" , "e" , "i" , "o" , "u" | |
puts "#{var} a vowel!" | |
else | |
puts "#{var} is not a vowel" | |
end | |
if var == "a" || if var == "e" || if var == "i" || if var == "o" || if var == "u" | |
puts "#{var} a vowel!" | |
else | |
puts "#{var} not a vowel!" | |
end | |
end | |
end | |
end | |
end | |
# Write a while loop that runs exactly 5 times | |
count = 5 | |
while count > 0 | |
puts "#{count}" | |
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. | |
count = 1 | |
while count <= 10 | |
puts "#{count}" if count.odd? | |
count += 1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment