Sha256: 882b4b776905f464a0c55678f9f73294b0650c0f5c8ad914f39c3363d85a3b41

Contents?: true

Size: 889 Bytes

Versions: 25

Compression:

Stored size: 889 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 unless method_defined?(:extractable_options?)
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 last.is_a?(Hash) && last.extractable_options?
      pop
    else
      {}
    end
  end unless method_defined?(:extract_options!)
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
gorillib-0.0.6 lib/gorillib/array/extract_options.rb
gorillib-0.0.5 lib/gorillib/array/extract_options.rb
gorillib-0.0.4 lib/gorillib/array/extract_options.rb
gorillib-0.0.3 lib/gorillib/array/extract_options.rb
gorillib-0.0.2 lib/gorillib/array/extract_options.rb