# Class: Counter

Inherits:
  Object
  - Object
  - Counter
  show all

Defined in:
  counter.rb

## Overview

Note:

This is only a minimal example

Implements a simple accumulator, whose value is accessed via the attribute counter. Calling the method Counter#inc increments this value.

Author:

- Dave Thomas

Version:

- 1.0

## Instance Attribute Summary collapse

- #counter ⇒ Object readonly

  The current value of the count.

## Instance Method Summary collapse

- #inc ⇒ Integer

  increment the current value of the count.

- #initialize(initial_value = 0) ⇒ Counter constructor

  create a new Counter with the given initial value.

## Constructor Details

### #initialize(initial_value = 0) ⇒ `Counter`

create a new Counter with the given initial value

Parameters:

- initial_value (`Integer`) (defaults to: `0`) —

  the initial value of the counter

    15
    16
    17
    # File 'counter.rb', line 15

    def initialize(initial_value = 0)
      @counter = initial_value
    end

## Instance Attribute Details

### #counter ⇒ `Object` (readonly)

The current value of the count

    10
    11
    12
    # File 'counter.rb', line 10

    def counter
      @counter
    end

## Instance Method Details

### #inc ⇒ `Integer`

increment the current value of the count

#### Examples:

#####

Increment the counter

    Counter.new.increment #=> counter.value == 1

Returns:

- (`Integer`) —

  The new value of the counter

    23
    24
    25
    # File 'counter.rb', line 23

    def inc
      @counter += 1
    end