Sha256: df1221105839fd710c9ae699f71e41ff4c6ffffd5e4b6f49691f6a98ddbb984c

Contents?: true

Size: 913 Bytes

Versions: 1

Compression:

Stored size: 913 Bytes

Contents

module Stig
  module Generators
    module Array
      # Public: Generates an random Array. Elements are generated by a 
      # generator. Size is variable, use a Range with equal start and end for a
      # fixed size.
      #
      # generator - An object implementing #call or #random.
      # size      - A maximum size Integer or Range (default: 1..10).
      #
      # Returns an Array.
      # Raises ArgumentError when an invalid generator was supplied.
      def self.random(generator, size = 1..10)
        unless generator.respond_to?(:call) || generator.respond_to?(:random)
          msg = "no #call or #random implemented for #{generator}"
          raise ArgumentError, msg
        end

        method = generator.respond_to?(:call) ? :call : :random

        array = []

        rand(size).times do
          array << generator.send(method)
        end

        array
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
stig-0.1.0 lib/stig/generators/array.rb