Sha256: 2a823889f4a3b7c933d87e44964c2b9936664dba02864c3c07af8a00c96a5618

Contents?: true

Size: 977 Bytes

Versions: 9

Compression:

Stored size: 977 Bytes

Contents

# copied from activesupport/core_ext from Rails, MIT license
# https://github.com/rails/rails/tree/5aa869861c192daceafe3a3ee50eb93f5a2b7bd2/activesupport/lib/active_support/core_ext
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 & 2 rubygems

Version Path
ruby-dbus-0.14.1 lib/dbus/core_ext/array/extract_options.rb
ruby-dbus-0.14.0 lib/dbus/core_ext/array/extract_options.rb
ruby-dbus-0.13.0 lib/dbus/core_ext/array/extract_options.rb
ruby-dbus-0.12.0 lib/dbus/core_ext/array/extract_options.rb
ruby-dbus-0.11.2 lib/dbus/core_ext/array/extract_options.rb
ruby-dbus-0.11.1 lib/dbus/core_ext/array/extract_options.rb
em-ruby-dbus-0.11.0 lib/dbus/core_ext/array/extract_options.rb
ruby-dbus-0.11.0 lib/dbus/core_ext/array/extract_options.rb
ruby-dbus-0.10.0 lib/dbus/core_ext/array/extract_options.rb