Add double_upto method to Fixnum : Fixnum extension « Number « Ruby






Add double_upto method to Fixnum


class Fixnum
  def double_upto(stop)
    x = self
    until x > stop
      yield x
     x = x * 2
    end
  end
end
10.double_upto(50) { |x| puts x }
# 10
# 20
# 40

 








Related examples in the same category

1.Calculating a factorial in Ruby
2.Simulating a Subclass of Fixnum
3.NoMethodError: undefined method 'new' for Fixnum:Class
4.redefine basic arithmetic