Sha256: 568d586afa2bd7dbd227a5f55d4ba31bdfa50a6e725646983a99eb91c9322395

Contents?: true

Size: 1.96 KB

Versions: 1

Compression:

Stored size: 1.96 KB

Contents

class Integer {
  """
  Base class for integer values in Fancy.
  """

  def times: block offset: offset {
    """
    @block @Block@ to be called with each number between @offset and @self.
    @offset Offset to be used as starting point of iteration.
    @return @self.

    Similar to #times: but starts at a given offset.
    """

    times: |i| {
      block call: [i + offset]
    }
  }

  def times_try: block retry_with: retry_block ({}) {
    """
    @block @Block@ to be called and retries @self amount of times if exceptions get thrown.
    @retry_block @Block@ to be called before retrying execution of @block. Defaults to an empty @Block@.
    @return Return value of calling @block or raises an exception after @self tries. Returns @nil if @self <= 0.

    Tries to call a @Block@ @self amount of times, returning its return
    value or raising the last exception raised from @block after @self tries.

    Example:
          2 times_try: {
            # this code will be tried 2 times.
            # if it succeeds the first time, simply return its value, otherwise try once more.
            # if it still fails after 2 attempts, raises the exception thrown (in this case probably an IOError).
            @connection readln
          } retry_with: { @connection reconnect }
    """

    max_retries = self
    { return nil } if: $ max_retries <= 0
    value = nil
    try {
      value = block call: [max_retries]
    } catch StandardError => e {
      max_retries = max_retries - 1
      { e raise! } unless: $ max_retries > 0
      retry_block call
      retry
    }
    value
  }

  def decimals {
    """
    @return @Array@ of all decimals of @self.

    Returns all decimals of an Integer as an Array.

    Example:
          100 decimals   # => [1, 0, 0]
          12345 decimals # => [1, 2, 3, 4, 5]
    """

    decimals = []
    tmp = self
    while: { tmp >= 10 } do: {
      decimals << (tmp modulo: 10)
      tmp = tmp div: 10
    }
    decimals << tmp
    decimals reverse
  }
}

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fancy-0.8.0 lib/integer.fy