Class: Hash

Inherits:
Object show all
Extended by:
Nuggets::Hash::NestMixin
Includes:
Nuggets::Hash::BlankMixin, Nuggets::Hash::UnrollMixin
Defined in:
lib/nuggets/hash/insert.rb,
lib/nuggets/hash/at.rb,
lib/nuggets/hash/nest.rb,
lib/nuggets/hash/only.rb,
lib/nuggets/hash/unroll.rb,
lib/nuggets/object/blank.rb,
lib/nuggets/hash/in_order.rb

Overview

#

A component of ruby-nuggets, some extensions to the Ruby programming # language. #

#

Copyright © 2007-2008 Jens Wille #

#

Authors: #

Jens Wille <jens.wille@uni-koeln.de>                                    #
                                                                        #

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

#

ruby-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 General Public License for # more details. #

#

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

#

++

Instance Method Summary collapse

Methods included from Nuggets::Hash::NestMixin

nest

Methods included from Nuggets::Hash::BlankMixin

#vain?

Methods included from Nuggets::Hash::UnrollMixin

#unroll

Instance Method Details

#at(what) ⇒ 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.



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/nuggets/hash/at.rb', line 37

def at(what)
  return {} if empty?

  key = case what
    when Integer
      keys[what]
    else
      block_given? ? keys.send(*what) { |*a| yield(*a) } : keys.send(*what)
  end

  { key => self[key] }
end

#firstObject

call-seq:

hash.first => aHash

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



54
55
56
# File 'lib/nuggets/hash/at.rb', line 54

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]]


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

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.



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

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) ⇒ Object

call-seq:

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

Destructive version of #insert.



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

def insert!(other)
  replace block_given? ? insert(other) { |*a| yield(*a) } : insert(other)
end

#lastObject

call-seq:

hash.last => aHash

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



62
63
64
# File 'lib/nuggets/hash/at.rb', line 62

def last
  at(:last)
end

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

call-seq:

hash.only(relax = true or false) => aHash

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

Raises:

  • (IndexError)


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

def only(relax = size == 1, split = false)
  raise IndexError, 'not a single-element hash' unless relax

  split ? Array(*first) : first
end

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

call-seq:

hash.only_pair(relax = true or false) => anArray

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



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.



70
71
72
# File 'lib/nuggets/hash/at.rb', line 70

def rand
  at(:rand)
end