Hey I was given the fizzbuzz task recently and I had answered with the usual,
if ((i%3==0) || (i.to_s.include?('3'))) && ((i%7==0) || (i.to_s.include?('7')))
p 'Fizzbuzz'
elsif (i%3==0) || (i.to_s.include?('3'))
p 'Fizz'
elsif (i%7==0) || (i.to_s.include?('7'))
p 'Buzz'
else
p i
end
and when asked to shorten it I tried using ternary operators:
p (i%3<1 || i.to_s.include?('3')) ? ((i%7<1 || i.to_s.include?('7')) ? "Fizzbuzz" : "Fizz") : ((i%7<1 || i.to_s.include?('7')) ? "Buzz" : i)
but when asked to solve it using the Enumerable methods(select,reject,collect etc) I was well stumped...Any body tried this before??
The select/collect methods were specificaly mentioned so I'm guessing that he had something like this in mind(excuse the crappy code) (1..100).select { |i| i % 3 == 0 }.collect { "fizz" } but i'm stuck when trying to do this for the 3 conditions and print out the result(ie iterate through the output array) :\