Skip to content

Instantly share code, notes, and snippets.

@FioFiyo
Created June 21, 2016 20:46
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 FioFiyo/40b6f1a31b17bb8c2ca187b20976ba0b to your computer and use it in GitHub Desktop.
Save FioFiyo/40b6f1a31b17bb8c2ca187b20976ba0b to your computer and use it in GitHub Desktop.
Using each method & range(up to) with case and not if statements.
# 1.upto(100) do |i|
# if i % 5 == 0 && i % 3 == 0
# puts "FizzBuzz"
# elsif i % 5 == 0
# puts "Buzz"
# elsif i % 3 == 0
# puts "Fizz"
# else
# puts i
# end
# end
------------------------------------
#you're giving me first number
print "Choose your first number:"
first = gets.chomp.to_i.abs
#you're giving me your last number
print "Choose your last number:"
last = gets.chomp.to_i.abs
first.upto(last) {|i|
case
when i % 5 == 0 && i % 3 == 0
puts "FizzBuzz"
when i % 5 == 0
puts "Buzz"
when i % 3 == 0
puts "Fizz"
else
puts i
end
}
#using each loop
numbers = (first..last).to_a
numbers.each {|number|
case number
when number % 5 == 0 && number % 3 == 0
puts "FizzBuzz"
when number % 5 == 0
puts "Buzz"
when number % 3 == 0
puts "Fizz"
else
puts number
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment