Skip to content

Instantly share code, notes, and snippets.

@quardox
Forked from reneedv/fizz_buzz.rb
Created June 12, 2012 19:14
Show Gist options
  • Save quardox/2919533 to your computer and use it in GitHub Desktop.
Save quardox/2919533 to your computer and use it in GitHub Desktop.
FizzBuzz Homework 1
# Write a Ruby script that prints the numbers from 1-100,
# replacing every multiple of 3 with the word Fizz,
# replacing every multiple of 5 with the word Buzz,
# and replacing every multiple of both with the word FizzBuzz
#
# My idea was to create an array of 1-100, then convert that to a string so I could use successive gsubs to replace the multiples with the correct words. When I try to run it I get "undefined method 'gsub' for #<Array>. I've discovered I'm a lot better at editing/adding to things that already exist than trying to set up a script from scratch. Not sure if I'm even on the right track here and I could definitely use your input. Thanks!
class FizzBuzz
array = Array (1..100)
def numbers
numbers = array.to_s
end
def Fizz
Fizz = numbers.gsub(/{|item| item % 3 == 0}/, 'Fizz')
end
def Buzz
Buzz = Fizz.gsub(/{|item| item % 5 == 0}/), 'Buzz'
end
def FizzBuzz
FizzBuzz = Buzz.gsub(/{|item| item % 15 == 0}/, 'FizzBuzz')
end
puts FizzBuzz
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment