Skip to content

Instantly share code, notes, and snippets.

@rainyjonne
Last active September 24, 2020 09:07
Show Gist options
  • Save rainyjonne/9b44965b4ca568949a746017e44b9d25 to your computer and use it in GitHub Desktop.
Save rainyjonne/9b44965b4ca568949a746017e44b9d25 to your computer and use it in GitHub Desktop.
fizz_buzz excercise 109078513
# frozen_string_literal: true
## write your fizzbuzz method in this file
# see http://en.wikipedia.org/wiki/Fizz_buzz for details on FizzBuzz game
def fizzbuzz(size, &decorate)
arr = Array.new(size) { |i| i + 1 }
fizz_buzz_arr = arr.map do |item|
if (item % 15).zero? then 'FizzBuzz'
elsif (item % 5).zero? then 'Buzz'
elsif (item % 3).zero? then 'Fizz'
else item.to_s
end
end
return fizz_buzz_arr unless decorate
fizz_buzz_arr.map { |item| yield item }
end
print fizzbuzz(30) { |item| "-#{item}-" }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment