Sha256: 47ae313ac9d5bd4f4ba191cba5c1fdea464fea0199e6339d60373708312d2f6b

Contents?: true

Size: 803 Bytes

Versions: 10

Compression:

Stored size: 803 Bytes

Contents

class Hash
  # By default, only instances of Hash itself are extractable.
  # Subclasses of Hash may implement this method and return
  # true to declare themselves as extractable. If a Hash
  # is extractable, Array#extract_options! pops it from
  # the Array when it is the last element of the Array.
  def extractable_options?
    instance_of?(Hash)
  end
end

class Array
  # Extracts options from a set of arguments. Removes and returns the last
  # element in the array if it's a hash, otherwise returns a blank hash.
  #
  #   def options(*args)
  #     args.extract_options!
  #   end
  #
  #   options(1, 2)           # => {}
  #   options(1, 2, :a => :b) # => {:a=>:b}
  #
  def extract_options!
    if Hash === last && last.extractable_options?
      pop
    else
      {}
    end
  end
end

Version data entries

10 entries across 9 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/array/extract_options.rb
facets-3.1.0 lib/core/facets/array/extract_options.rb
facets-3.0.0 lib/core/facets/array/extract_options.rb
facets-2.9.3 lib/core/facets/array/extract_options.rb
facets-2.9.2 src/core/facets/array/extract_options.rb
facets-2.9.2 lib/core/facets/array/extract_options.rb
facets-2.9.1 lib/core/facets/array/extract_options.rb
facets-2.9.0 lib/core/facets/array/extract_options.rb
facets-2.9.0.pre.2 lib/core/facets/array/extract_options.rb
facets-2.9.0.pre.1 lib/core/facets/array/extract_options.rb