Sha256: bed5908169dd34b2d663882d931c7085356eb355def2eb348f39fbce08e4024c

Contents?: true

Size: 886 Bytes

Versions: 2

Compression:

Stored size: 886 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 unless Hash.respond_to?(:extractable_options?)

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
end unless Array.respond_to?(:extract_options!)

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_enumerable-1.1.0 lib/active_enumerable/extract_options.rb
active_enumerable-1.0.0 lib/active_enumerable/extract_options.rb