Created
January 10, 2018 01:03
-
-
Save nicholalexander/44f63f5b6dcc0e035c546ba7183a275b to your computer and use it in GitHub Desktop.
cracklepop
This file contains hidden or 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
# Write a program that prints out the numbers 1 to 100 (inclusive). If the number is | |
# divisible by 3, print Crackle instead of the number. If it's divisible by 5, print Pop. | |
# If it's divisible by both 3 and 5, print CracklePop. You can use any language. | |
module CracklePop | |
class CracklePop | |
class << self | |
def evaluate_and_print(number) | |
puts evaluate(number) | |
end | |
private | |
def evaluate(number) | |
return "CracklePop" if crackable?(number) && poppable?(number) | |
return "Crackle" if crackable?(number) | |
return "Pop" if poppable?(number) | |
number | |
end | |
def crackable?(number) | |
number % 3 == 0 | |
end | |
def poppable?(number) | |
number % 5 == 0 | |
end | |
end | |
end | |
refine Integer do | |
def to_crackle_pop | |
CracklePop.evaluate_and_print(self) | |
end | |
end | |
end | |
class Blurgh | |
using CracklePop | |
def self.run | |
1.upto 100 do |number| | |
number.to_crackle_pop | |
end | |
end | |
end | |
Blurgh.run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment