Sha256: c75136838dec7fd083b8e434f148c9d5dcf8db9266d523f2f6e936b957b89224

Contents?: true

Size: 1023 Bytes

Versions: 5

Compression:

Stored size: 1023 Bytes

Contents

# @note copied from the main Rails repo instead of adding via active_support
# @see https://github.com/rails/rails/blob/794a70f94485fb64ed1c49ba8532895306e2001c/activesupport/lib/active_support/core_ext/array/extract_options.rb
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 last.is_a?(Hash) && last.extractable_options?
      pop
    else
      {}
    end
  end
end

Version data entries

5 entries across 5 versions & 3 rubygems

Version Path
ico-0.1.2 lib/core_ext/array/extract_options.rb
ico-0.1.1 lib/core_ext/array/extract_options.rb
ico-0.1.0 lib/core_ext/array/extract_options.rb
bmp-ruby-0.1.1 lib/core_ext/array/extract_options.rb
svelte-ruby-0.1.0 lib/core_ext/array/extract_options.rb