Guest User

Untitled

a guest
Oct 19th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. # Write a Ruby script that prints the numbers from 1-100,
  2. # replacing every multiple of 3 with the word Fizz,
  3. # replacing every multiple of 5 with the word Buzz,
  4. # and replacing every multiple of both with the word FizzBuzz
  5. #
  6. # 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!
  7.  
  8. class FizzBuzz
  9. array = Array (1..100)
  10.  
  11. def numbers
  12. numbers = array.to_s
  13. end
  14.  
  15. def Fizz
  16. Fizz = numbers.gsub(/{|item| item % 3 == 0}/, 'Fizz')
  17. end
  18.  
  19. def Buzz
  20. Buzz = Fizz.gsub(/{|item| item % 5 == 0}/), 'Buzz'
  21. end
  22.  
  23. def FizzBuzz
  24. FizzBuzz = Buzz.gsub(/{|item| item % 15 == 0}/, 'FizzBuzz')
  25. end
  26.  
  27. puts FizzBuzz
  28. end
Add Comment
Please, Sign In to add comment