段差つき繰り返し(Ruby)

しょうもない実装です。何かに使うかなあ。

class Integer
  def upto_with_steps(max, step)
    i = self
    while i <= max
      yield(i)
      i += step
    end
  end

  def downto_with_steps(min, step)
    i = self
    while i >= min
      yield(i)
      i -= step
    end  
  end
end

1.upto_with_steps(16, 3) {|i| print "#{i} "}        #=>1 4 7 10 13 16
100.downto_with_steps(60, 7) {|i| print "#{i} "}    #=>100 93 86 79 72 65



追記:既に標準で実装されていました。
instance method Numeric#step (Ruby 2.2.0)