I would like to make a function so each time it is called it return a value that is incremented by 1 starting from 1.
I guess it could be made with a global variable, but not a pretty solution.
I would like to make a function so each time it is called it return a value that is incremented by 1 starting from 1.
I guess it could be made with a global variable, but not a pretty solution.
I'd probably do it this way:
class EverreadyBunnyCounter
def initialize
@counter = 0
end
def current
@counter
end
def next
@counter += 1
end
end
foo = EverreadyBunnyCounter.new
bar = EverreadyBunnyCounter.new
foo.next # => 1
bar.next # => 1
foo.next # => 2
bar.current # => 1
The current
method isn't necessary but it's sometimes convenient to be able to peek at the current value without forcing it to increment.
Alternately, this might do it:
MAX_INT = (2**(0.size * 8 -2) -1)
counter = (1..MAX_INT).to_enum # => #<Enumerator: 1..4611686018427387903:each>
foo = counter.dup
bar = counter.dup
foo.next # => 1
foo.next # => 2
bar.next # => 1
bar.next # => 2
foo.next # => 3
Defining MAX_INT
this way comes from "Ruby max integer". The downside is that you'll eventually run out of values because of the range being used to create the Enumerator, where the previous version using the EverreadyBunnyCounter
class will keep on going.
Changing MAX_INT
to Float::INFINITY
would be a way to fix that:
counter = (1..Float::INFINITY).to_enum # => #<Enumerator: 1..Infinity:each>
foo = counter.dup
bar = counter.dup
foo.next # => 1
foo.next # => 2
bar.next # => 1
bar.next # => 2
foo.next # => 3
The Enumerator documentation has more information.
Something like this?
def incr
@n ||= 0
@n += 1
end
incr
#=> 1
incr
#=> 2