Sha256: a43688490bb85550033e188133dc83857d265f6fa741cf189116877f15fc36f7

Contents?: true

Size: 794 Bytes

Versions: 9

Compression:

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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
html_terminator-6.1.0 lib/html_terminator/extract_options.rb
html_terminator-6.0.1 lib/html_terminator/extract_options.rb
html_terminator-6.0.0 lib/html_terminator/extract_options.rb
html_terminator-5.0.0 lib/html_terminator/extract_options.rb
html_terminator-4.0.0 lib/html_terminator/extract_options.rb
html_terminator-3.0.0 lib/html_terminator/extract_options.rb
html_terminator-2.0.2 lib/html_terminator/extract_options.rb
html_terminator-2.0.1 lib/html_terminator/extract_options.rb
html_terminator-2.0.0 lib/html_terminator/extract_options.rb