Sha256: cde2f6606be4b74b7d55dd04592f7b5c0b18d0b957e1e2bf72a4967764d6ebc2

Contents?: true

Size: 732 Bytes

Versions: 15

Compression:

Stored size: 732 Bytes

Contents

# frozen_string_literal: true

class ReeArray::Wrap
  include Ree::FnDSL

  fn :wrap

  doc(<<~DOC)
    Wraps its argument in an array unless it is already an array (or array-like).

    Specifically:

    * If the argument is +nil+ an empty array is returned.
    * Otherwise, if the argument responds to +to_ary+ it is invoked, and its result returned.
    * Otherwise, returns an array with the argument as its single element.

        wrap(nil)       # => []
        wrap([1, 2, 3]) # => [1, 2, 3]
        wrap(0)         # => [0]
  DOC
  contract(Any => ArrayOf[Any])
  def call(object)
    if object.nil?
      []
    elsif object.is_a?(Array) || object.is_a?(Enumerable)
      object
    else
      [object]
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
ree_lib-1.0.124 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.123 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.122 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.121 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.120 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.119 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.118 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.117 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.116 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.115 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.114 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.113 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.112 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.111 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.110 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb