Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/redisarray/core_ext/hash/reverse_merge.rb,
lib/redisarray/core_ext/array/extract_options.rb

Instance Method Summary collapse

Instance Method Details

#extractable_options?Boolean

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.

Returns:

  • (Boolean)


7
8
9
# File 'lib/redisarray/core_ext/array/extract_options.rb', line 7

def extractable_options?
  instance_of?(Hash)
end

#reverse_merge(other_hash) ⇒ Object

Merges the caller into other_hash. For example,

options = options.reverse_merge(:size => 25, :velocity => 10)

is equivalent to

options = {:size => 25, :velocity => 10}.merge(options)

This is particularly useful for initializing an options hash with default values.



12
13
14
# File 'lib/redisarray/core_ext/hash/reverse_merge.rb', line 12

def reverse_merge(other_hash)
  other_hash.merge(self)
end

#reverse_merge!(other_hash) ⇒ Object Also known as: reverse_update

Destructive reverse_merge.



17
18
19
20
# File 'lib/redisarray/core_ext/hash/reverse_merge.rb', line 17

def reverse_merge!(other_hash)
  # right wins if there is no left

  merge!(other_hash) { |key, left, right| left }
end