Skip to content

Instantly share code, notes, and snippets.

@Jaltman429
Created June 9, 2013 19:47
Show Gist options
  • Save Jaltman429/5744934 to your computer and use it in GitHub Desktop.
Save Jaltman429/5744934 to your computer and use it in GitHub Desktop.
logic.rb
# assignment.rb
#
# write an expression that returns true by using ==
# write an expression that returns false using ==
# write an expression that returns true using !=
# write an expression that returns false using !=
puts 4 == 2 * 2
puts 'jack' == 'JACK'
puts 'jack' != 'blob'
puts 4 != 4
# Write an if statement with 3 different branches
# use != in the first branch, == in the second, and > in the third
#
jack_name = 'gwen'
if jack_name != 'gwen'
puts 'different'
elsif jack_name == 'gwen'
puts 'same'
elsif jack_name.length > 3
puts 'long'
end
# Assign a variable based on the result of an if statement
#
# Execute code based on the result of an if statement.
# conditionally run puts "Hello Class" if 1 < 2
#
# Try using an if statement at the end of an expression
name = 'jack' if 4 > 3
# Write an if statement that negates the or operator to return true
if true || false
true
end
# Write an if statement that uses the and operator to create a false return
if true && 'jack'
false
end
#
# Write a Case Statement that checks if a variable is a vowel
vowel = 'u'
case vowel
when 'a', 'e', 'i', 'o', 'u'
true
else
puts "#{vowel} isn't a vowel"
end
# Rewrite that same case statement as an if statement
vowel = 'u'
if vowel = 'a' || vowel = 'e' || vowel = 'i' || vowel = 'o' || vowel = 'u'
true
else
puts "#{vowel} isn't a vowel"
end
# Write a Case statement that has at 4 branches and a default return
age = 29
case age
when 23
"lucky"
when 21
"drinking"
when 18
"legal"
when 25
"rent a car"
else
"boring"
end
# Assignment
# Write a while loop that runs exactly 5 times
number = 1
while number < 6
puts "current value is #{number}"
number += 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
x = 0
while x < 10
x += 1
if x % 2 != 0
puts x
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment