Sha256: d92b8ee4327d051179635063c0ba2df0d564fafe7a21e58a7ac9076ca6e75744

Contents?: true

Size: 716 Bytes

Versions: 109

Compression:

Stored size: 716 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
    else
      [object]
    end
  end
end

Version data entries

109 entries across 109 versions & 1 rubygems

Version Path
ree_lib-1.0.109 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.108 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.107 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.106 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.105 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.104 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.103 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.102 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.101 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.100 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.99 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.98 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.97 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.96 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.95 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.94 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.93 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.92 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.91 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb
ree_lib-1.0.90 lib/ree_lib/packages/ree_array/package/ree_array/functions/wrap.rb