Sha256: eb7ff552dbd9451e62e1e1b94f39c7e96b80dbc672ad3ad0e9363b5dee579040

Contents?: true

Size: 1.06 KB

Versions: 3

Compression:

Stored size: 1.06 KB

Contents

require "active_support/core_ext/array/extract_options"

class Array
  # Extracts options from a set of arguments. 
  #
	# @example
  #
  #   dirs, o = ["foo", "bar", {a: 1}].extract_options(b: 2)  
  #     -> ["foo", "bar"], {a: 1, b: 2}
  #
  #   (dir,), o = ["foo", {a: 1}].extract_options 
  #     -> "foo", {a: 1}
  #
	# @return [Array<Array,Hash>] 
	# @see extract_options!
	def extract_options(default={})
    if last.is_a?(Hash) && last.extractable_options?
			[self[0...-1], default.merge(self[-1])]
		else
			[self, default]
		end
	end

  # 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.
  #
  # @example
  #
  #   def options(*args)
  #     args.extract_options!(a: 1)
  #   end
  #
  #   options(1, 2)            -> {a: 1}
  #   options(1, 2, a: 2)      -> {a: 2}
	#
	# @param [Hash] default default options
	# @return [Hash]
	def extract_options!(default={})
    if last.is_a?(Hash) && last.extractable_options?
      default.merge pop
    else
      default
    end
	end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
tagen-2.0.2 lib/tagen/core/array/extract_options.rb
tagen-2.0.1 lib/tagen/core/array/extract_options.rb
tagen-2.0.0 lib/tagen/core/array/extract_options.rb