Class: Hash

Inherits:
Object show all
Extended by:
Nuggets::Hash::IDMapMixin, Nuggets::Hash::NestMixin, Nuggets::Hash::SeenMixin, Nuggets::Hash::ZipMixin
Includes:
Nuggets::Hash::BlankMixin, Nuggets::Hash::DeepFetchMixin, Nuggets::Hash::DeepMergeMixin, Nuggets::Hash::DeprocMixin, Nuggets::Hash::UnrollMixin
Defined in:
lib/nuggets/hash/insert.rb,
lib/nuggets/hash/at.rb,
lib/nuggets/hash/zip.rb,
lib/nuggets/hash/nest.rb,
lib/nuggets/hash/only.rb,
lib/nuggets/hash/seen.rb,
lib/nuggets/hash/idmap.rb,
lib/nuggets/hash/deproc.rb,
lib/nuggets/hash/unroll.rb,
lib/nuggets/object/blank.rb,
lib/nuggets/hash/in_order.rb,
lib/nuggets/hash/deep_fetch.rb,
lib/nuggets/hash/deep_merge.rb

Overview

#

nuggets – Extending Ruby #

#

Copyright © 2007-2011 Jens Wille #

#

Authors: #

Jens Wille <jens.wille@gmail.com>                                       #
                                                                        #

nuggets is free software; you can redistribute it and/or modify it under # the terms of the GNU Affero General Public License as published by the Free # Software Foundation; either version 3 of the License, or (at your option) # any later version. #

#

nuggets is distributed in the hope that it will be useful, but WITHOUT ANY # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for # more details. #

#

You should have received a copy of the GNU Affero General Public License # along with nuggets. If not, see <www.gnu.org/licenses/>. #

#

++

Direct Known Subclasses

Nuggets::Hash::ZipMixin::ZipHash

Instance Method Summary collapse

Methods included from Nuggets::Hash::ZipMixin

zip, zipkey, zipval

Methods included from Nuggets::Hash::NestMixin

array, identity, nest

Methods included from Nuggets::Hash::SeenMixin

seen

Methods included from Nuggets::Hash::IDMapMixin

idmap

Methods included from Nuggets::Hash::DeepMergeMixin

#deep_merge, #deep_merge!

Methods included from Nuggets::Hash::DeepFetchMixin

#deep_fetch

Methods included from Nuggets::Hash::BlankMixin

#vain?

Methods included from Nuggets::Hash::UnrollMixin

#unroll

Methods included from Nuggets::Hash::DeprocMixin

#deproc, #deproc!

Instance Method Details

#at(what, &block) ⇒ Object

call-seq:

hash.at(what) => aHash

Returns the key/value pair of hash at key position what. Remember that hashes might not have the intended (or expected) order in pre-1.9 Ruby.



36
37
38
39
40
41
42
# File 'lib/nuggets/hash/at.rb', line 36

def at(what, &block)
  return {} if empty?

  key = what.is_a?(::Integer) ? keys[what] : keys.send(*what, &block)

  { key => self[key] }
end

#firstObject

call-seq:

hash.first => aHash

Returns the “first” key/value pair of hash.



48
49
50
# File 'lib/nuggets/hash/at.rb', line 48

def first
  at(:first)
end

#in_order(*ordered) ⇒ Object

call-seq:

hash.in_order(*ordered) => anArray

Returns hash#to_a, in forced order (cf. Array#in_order).

Examples:

{ :a => 1, :b => 2, :c => 3 }.in_order(:b, :c)  #=> [[:b, 2], [:c, 3], [:a, 1]]
{ :a => 1, :b => 2, :c => 3 }.in_order(:b, :d)  #=> [[:b, 2], [:a, 1], [:c, 3]]


39
40
41
# File 'lib/nuggets/hash/in_order.rb', line 39

def in_order(*ordered)
  keys.in_order(*ordered).map { |key| [key, self[key]] }
end

#insert(other, &block) ⇒ Object

call-seq:

hash.insert(other) => new_hash
hash.insert(other) { |key, old_value, new_value| ... } => new_hash

Inserts other into hash, while merging existing values instead of just overwriting. Uses default Hash#merge or block for merging.



35
36
37
38
39
40
41
42
# File 'lib/nuggets/hash/insert.rb', line 35

def insert(other, &block)
  block ||= lambda { |key, old_val, new_val|
    old_val.is_a?(::Hash) && new_val.is_a?(::Hash) ?
      old_val.merge(new_val, &block) : new_val
  }

  merge(other, &block)
end

#insert!(other, &block) ⇒ Object

call-seq:

hash.insert!(other) => hash
hash.insert!(other) { |key, old_value, new_value| ... } => hash

Destructive version of #insert.



49
50
51
# File 'lib/nuggets/hash/insert.rb', line 49

def insert!(other, &block)
  replace(insert(other, &block))
end

#lastObject

call-seq:

hash.last => aHash

Returns the “last” key/value pair of hash.



56
57
58
# File 'lib/nuggets/hash/at.rb', line 56

def last
  at(:last)
end

#only(relax = size == 1, split = false) ⇒ Object

call-seq:

hash.only => aHash
hash.only(+true+) => aHash

Returns the only key/value pair of hash. Raises an IndexError if hash’s size is not 1, unless parameter true is passed.



37
38
39
40
# File 'lib/nuggets/hash/only.rb', line 37

def only(relax = size == 1, split = false)
  relax ? split ? Array(*first) : first :
    raise(::IndexError, 'not a single-element hash')
end

#only_pair(relax = size == 1) ⇒ Object

call-seq:

hash.only_pair => anArray
hash.only_pair(+true+) => anArray

Returns the only key/value pair of hash as an array. Raises an IndexError if hash’s size is not 1, unless parameter true is passed.



48
49
50
# File 'lib/nuggets/hash/only.rb', line 48

def only_pair(relax = size == 1)
  only(relax, true)
end

#randObject

call-seq:

hash.rand => aHash

Returns a random key/value pair of hash.



64
65
66
# File 'lib/nuggets/hash/at.rb', line 64

def rand
  at(:rand)
end