vadviktor
1/16/2017 - 4:19 PM

Ruby generator producing multiples of a number up to a specified value

Ruby generator producing multiples of a number up to a specified value

require 'pp'

def multiples_of_number_up_to(n, upto)
  i = n
  Enumerator.new do |y|
    loop do
      y << i
      i += n
      break if i >= upto
    end
  end
end

pp multiples_of_number_up_to(3, 21).to_a