Sha256: bb1c19391e5a899858d75f63608fc6088825961a3329f5c183b5d61f6bbba603

Contents?: true

Size: 826 Bytes

Versions: 234

Compression:

Stored size: 826 Bytes

Contents

# frozen_string_literal: true

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

234 entries across 219 versions & 29 rubygems

Version Path
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/core_ext/array/extract_options.rb
zuora_connect_ui-0.8.3 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/core_ext/array/extract_options.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/core_ext/array/extract_options.rb
zuora_connect_ui-0.8.2 vendor/ruby/2.6.0/gems/activesupport-6.0.0/lib/active_support/core_ext/array/extract_options.rb
activesupport-6.0.0 lib/active_support/core_ext/array/extract_options.rb
zuora_connect_ui-0.8.1 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/core_ext/array/extract_options.rb
zuora_connect_ui-0.8.0 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/core_ext/array/extract_options.rb
spiral_form-0.1.1 vendor/bundle/gems/activesupport-5.2.3/lib/active_support/core_ext/array/extract_options.rb
spiral_form-0.1.0 vendor/bundle/gems/activesupport-5.2.3/lib/active_support/core_ext/array/extract_options.rb
activesupport-6.0.0.rc2 lib/active_support/core_ext/array/extract_options.rb
zuora_connect_ui-0.7.1 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/core_ext/array/extract_options.rb
zuora_connect_ui-0.7.0 vendor/ruby/2.6.0/gems/activesupport-5.2.3/lib/active_support/core_ext/array/extract_options.rb
ric-0.13.0 vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.3/lib/active_support/core_ext/array/extract_options.rb
ric-0.12.2 vendor/bundle/ruby/2.5.0/gems/activesupport-5.2.3/lib/active_support/core_ext/array/extract_options.rb
activesupport-6.0.0.rc1 lib/active_support/core_ext/array/extract_options.rb
activesupport-5.2.3 lib/active_support/core_ext/array/extract_options.rb
activesupport-5.2.3.rc1 lib/active_support/core_ext/array/extract_options.rb
activesupport-6.0.0.beta3 lib/active_support/core_ext/array/extract_options.rb
activesupport-5.2.2.1 lib/active_support/core_ext/array/extract_options.rb
activesupport-6.0.0.beta2 lib/active_support/core_ext/array/extract_options.rb