Skip to content

Instantly share code, notes, and snippets.

@FrancoB411
Created March 7, 2012 06:58
Show Gist options
  • Select an option

  • Save FrancoB411/1991551 to your computer and use it in GitHub Desktop.

Select an option

Save FrancoB411/1991551 to your computer and use it in GitHub Desktop.
Fizzbuzz that also calls out prime numbers, by extending the Fixnum class with a .prime? method.
#extends Fixnum class with prim? method. usage ex: 5.prime?
#returns a boolean
class Fixnum
def prime?
('1' * self) !~ /^1?$|^(11+?)\1+$/
end
end
nums = (1..100)
#Fizzbuzz as if statements
=begin
nums.each do |num|
if num.prime?
puts "Priiime!"
elsif num%3 == 0 && num%5 == 0
puts "fizzbuzz"
elsif num%3 == 0
puts "fizz"
elsif num%5 == 0
puts "buzz"
else
puts num
end
end
=end
#Fizzbuzz as case expression
nums.each do |num|
case
when num.prime?
puts "Priiime!"
when num%3 == 0 && num%5 == 0
puts "fizzbuzz"
when num%3 == 0
puts "fizz"
when num%5 == 0
puts "buzz"
else
puts num
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment