lib/dry/monads/list.rb
# Coerces a value to a list. `nil` will be coerced to an empty list.
#
# @param value [Object] Value
# @param type [Monad] Embedded monad type (used in case of list of monadic values)
# @return [List]
Coerces a value to a list. `nil` will be coerced to an empty list.
Coerces a value to a list . ` nil ` will be coerced to an empty list .
def coerce(value, type = nil)
          if value.nil?
            List.new([], type)
          elsif value.respond_to?(:to_ary)
            values = value.to_ary
            if !values.empty? && type.nil? && values[0].respond_to?(:monad)
              List.new(values, values[0].monad)
            else
              List.new(values, type)
            end
          else
            raise TypeError, "Can't coerce #{value.inspect} to List"
          end
        end
def coerce ( value , type = nil ) if value . nil? List . new ( [ ] , type ) elsif value . respond_to? ( :to_ary ) values = value . to_ary if ! values . empty? && type . nil? && values [ 0 ] . respond_to? ( :monad ) List . new ( values , values [ 0 ] . monad ) else List . new ( values , type ) end else raise TypeError , "Can't coerce #{value.inspect} to List" end end
Coerces a value to a list.
Coerces a value to a list .
None
List
value
None
type
Embedded monad type (used in case of list of monadic values)
Embedded monad type ( used in case of list of monadic values )