Skip to content

Instantly share code, notes, and snippets.

@sobopla
Last active September 30, 2018 21:14
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 sobopla/60d76a7422778f0ea7b81801b963855e to your computer and use it in GitHub Desktop.
Save sobopla/60d76a7422778f0ea7b81801b963855e to your computer and use it in GitHub Desktop.
customfizzbuzz
class CustomFizzBuzz
attr_accessor :numberset, :input, :custom_words
def initialize
@input = userinput
@numberset = setnumberset
@custom_words = {}
end
def userinput
gets.chomp.split(',')
end
def setnumberset #?
array = @input.map {|i| i.to_i}
if islist?(array)
@numberset = array
else
@numberset = createrange(array)
end
end
def custominput
wordinput = userinput
hash = to_hash(wordinput)
@custom_words = hash_values_to_i(hash)
end
def hash_values_to_i(hash) #turn the hash values into integers.
hash.each {|k,v| hash[k] = v.to_i}
end
def to_hash(array)
Hash[*array.flatten(1)]
end
def createrange(arr) #create range from two numbers
(arr[0]..arr[1]).to_a
end
def islist?(arr) #check if input is 2 nums or a list
arr.length > 2
end
def fizzbuzzit #do simple fizzbuzz
@numberset.each do |i|
str = ''
str += 'Fizz' if is_divisible_by_three?(i)
str += 'Buzz' if is_divisible_by_five?(i)
str += i.to_s if str.empty?
puts str
end
end
def customfizzbuzzit
@numberset.each do |i|
str = ''
@custom_words.each do |word, divisible|
str += word if i%divisible ==0
end
str += i.to_s if str.empty?
puts str
end
end
def extrafizzbuzzit
end
def is_divisible_by_three?(i)
i % 3 == 0
end
def is_divisible_by_five?(i)
i % 5 == 0
end
def run
self.userinput
self.fizzbuzzit
puts "Enter your custom words and the division number you want to test. Separate with
comas in a list"
self.custominput
self.custom_words
self.customfizzbuzzit
end
end #
puts "User, enter two numbers separated by a comma to specify a range with the smallest number first.
If you enter more than two numbers, all the numbers will be tested.
Entering spaces is not valid and there are no errors to tell you so. Enter your numbers:"
#input = gets.chomp
game = CustomFizzBuzz.new
game.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment