Module: Ratomic::CounterMethods

Defined in:
lib/ratomic/counter.rb

Overview

Ruby convenience methods for Counter.

Instance Method Summary collapse

Instance Method Details

#dec(amt = 1) ⇒ void

This method returns an undefined value.

Decrement the counter.

Parameters:

  • amt (Integer) (defaults to: 1)

    amount to subtract

Raises:

  • (ArgumentError)

    if amt is negative



43
44
45
46
47
# File 'lib/ratomic/counter.rb', line 43

def dec(amt = 1)
  raise ArgumentError, "amount must be positive: #{amt}" if amt.negative?

  decrement(amt)
end

#inc(amt = 1) ⇒ void

This method returns an undefined value.

Increment the counter.

Parameters:

  • amt (Integer) (defaults to: 1)

    amount to add

Raises:

  • (ArgumentError)

    if amt is negative



32
33
34
35
36
# File 'lib/ratomic/counter.rb', line 32

def inc(amt = 1)
  raise ArgumentError, "amount must be positive: #{amt}" if amt.negative?

  increment(amt)
end

#to_iInteger

Coerce the counter to an Integer snapshot.

Returns:

  • (Integer)


16
17
18
# File 'lib/ratomic/counter.rb', line 16

def to_i
  read
end

#valueInteger

Read the current counter value.

Returns:

  • (Integer)


9
10
11
# File 'lib/ratomic/counter.rb', line 9

def value
  read
end

#zero?Boolean

Check whether the current counter value is zero.

Returns:

  • (Boolean)


23
24
25
# File 'lib/ratomic/counter.rb', line 23

def zero?
  read.zero?
end