Spoiler alert: I am a true novice. Tasked with figuring out fizz buzz in ruby for a class and while I have found more than a few versions of code that solve the problem, my understanding is so rudimentary that I cannot figure out how these examples truly work.
First question(refer to spoiler alert if you laugh out loud at this): How do i print out numbers one through 100 in Ruby?
Second question: can 'if else" be used to solve this? My failed code is below(attachment has screen shot):
puts('Lets play fizzbuzz')
print('enter a number: ')
number = gets()
puts(number)
if number == % 3
puts ('fizz')
elsif number == % 5
puts ('buzz')
elsif number == %15
puts ('fizzbuzz')
end
Thanks,
Not the most beautiful way to write it but good for beginners and for readability.
First question:
another method that can be helpful :
First question: this problem has several solutions. For example,
For true novice: https://github.com/bbatsov/ruby-style-guide
Here is my most "idiomatic ruby" solution:
And it works:
https://gist.github.com/galori/47db94ecb822de2ac17c
Thats ok being a novice, we all have to start somewhere right? Ruby is lovely as it get us to use blocks all the time, so to count to 100 you can use several methods on fixnum, look at the docs for more. Here is one example which might help you;
For your second question maybe take a quick look at the small implementation i whipped up for you, it hopefully might help you understand this problem: