I'm trying to automate a test. Is there a better approach to solving this? I'm trying to create an array that only contains multiples of three and nothing else.
(1...100).each_with_object([]) { |i, a| a << i if i % 3 == 0 }.reject { |i| i % 5 == 0 }
Why not map the digits 1 to 33 by multiplying each by three, then rejecting the multiples of 5?
(1..33).map { |i| i * 3 }.reject { |i| i % 5 == 0 }
(1.33).map{|i| i * 3 unless i * 3 % 5 ==0}.compact!I'm trying to create an array that only contains multiples of three and nothing else.
Using the Numeric#step enumerator:
3.step(100, 3).to_a
creates all multiples of three up to 100.
Your code seems to have an additional effect of filtering out the fives too. You can filter them out:
3.step(100, 3).reject { |i| i % 5 == 0 }
Or you can do something completely different:
require 'set'
((1..100).to_set - 3.step(100, 3) - 5.step(100, 5)).to_a
A bit more legible than the reject way, but probably a bit slower.
(1..100).to_set can also be written Set.new(1..100). I wasn't aware that to_set could be used to convert an Enumerable object (and, hence, Enumerator objects such as 3.step(100,3), since Enumerator includes Enumerable) to a set, but [that it does](http://www.ruby-doc.org/stdlib-2.1.1/libdoc/set/rdoc/Set.html), but I still don't understand why there is no Enumerable#to_set. I only find Set.to_set`.A million and 1 ways to iterate in ruby
(1..100).collect{|i| i if i % 3 == 0 && i % 5 != 0}.compact!
(1..100).select{|i| i if i % 3 == 0 && i % 5 != 0}
Here's another one and it reads really nicely
(1..100).find_all{|i| i % 3 ==0 && i % 5 !=0}
collect doesn't really fit here; use select or rejectcompact! added select as well
xdivides evenly by 3 to givey,xwill divide evenly byyto give 3. There are no numbers except for 3 and 9 which meet your criteria of being "multiples of 3 and nothing else" and that's assuming you'll disregard that every number is a multiple of 1.[3, 6, 9].each { |x| expect(fizzbuzz(x)).to eq('Fizz') }