Class: Hash

Inherits:
Object show all
Defined in:
lib/hash-utils/hash.rb

Overview

Hash extension.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.combine(keys, values) ⇒ Hash

Combines two arrays to Hash. First array will be keys, second one will be values.

Examples:

keys = [:a, :b, :c]
values = [1, 2, 3]

Hash::combine(keys, values)     # will return {:a => 1, :b => 2, :c => 3}

Parameters:

Returns:

  • (Hash)

    resultant hash

Since:

  • 0.10.0



499
500
501
502
503
504
505
506
# File 'lib/hash-utils/hash.rb', line 499

def self.combine(keys, values)
    result = { }
    keys.each_index do |i|
        result[keys[i]] = values[i]
    end
    
    return result
end

.create(default = nil, hash = { }, &block) ⇒ Hash

Creates hash by setting default settings in one call.

Parameters:

  • values (Hash)

    initial values

  • default (Object) (defaults to: nil)

    default value

  • block (Proc)

    default block

Returns:

  • (Hash)

    new hash

Since:

  • 0.3.0



36
37
38
39
40
41
42
43
44
# File 'lib/hash-utils/hash.rb', line 36

def self.create(default = nil, hash = { }, &block)
    hash.default = default
    
    if not block.nil?
        hash.default_proc = block
    end
    
    return hash        
end

.define(values = { }, default = nil, &block) ⇒ Hash

Defines hash by setting the default value or an Proc and content.

Parameters:

  • values (Hash) (defaults to: { })

    initial values

  • default (Object) (defaults to: nil)

    default value

  • block (Proc)

    default block

Returns:

  • (Hash)

    new hash

Since:

  • 0.3.0



21
22
23
24
# File 'lib/hash-utils/hash.rb', line 21

def self.define(values = { }, default = nil, &block) 
    hash = self[values]
    self::create(default, hash, &block)
end

Instance Method Details

#all?(&block) ⇒ Boolean

Deprecated.

(since 0.10.0, conflict with built-in method)

Note:

This method is currently in conflict with Ruby 1.9.2 Hash#all? method. Given block arity is checked, so code should be compatible for now, but this method will be probably moved around 0.13.0 for performance reasons of sure to deprecated module.

Checks, all elements values follow condition expressed in block. Block must return boolean.

If it’s empty, returns true.

Parameters:

  • block (Proc)

    checking block

Returns:

  • (Boolean)

    true if yes, false in otherwise

Since:

  • 0.2.0



261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/hash-utils/hash.rb', line 261

def all?(&block)
    if block.arity == 2
        self.all_pairs? &block
    end
    
    if self.empty? or block.nil?
        return true
    end
    
    self.each_value do |v|
        if block.call(v) == false
            return false
        end
    end
    
    return true
end

#all_pairs?(&block) ⇒ Boolean

Note:

Since version 0.13.0 will be replaced by an alias to Ruby built-in #all?.

Checks, all elements follow condition expressed in block. Block must return boolean.

If it’s empty, returns true.

Parameters:

  • block (Proc)

    checking block

Returns:

  • (Boolean)

    true if yes, false in otherwise

Since:

  • 0.2.0



292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/hash-utils/hash.rb', line 292

def all_pairs?(&block)
    if self.empty?
        return true
    end
    
    self.each_pair do |k, v|
        if block.call(k, v) == false
            return false
        end
    end
    
    return true
end

#asort(&block) ⇒ Hash

Sorts hash according to values. Keeps key associations. It’s equivalent of PHP asort().

Be warn, this method have sense in Ruby 1.9 only. Ruby 1.8 doesn’t maintain pair order in Hashes.

Parameters:

  • block (Proc)

    comparing block (see similar for #sort method)

Returns:

  • (Hash)

    new sorted hash

See Also:

Since:

  • 0.5.0



400
401
402
403
404
405
# File 'lib/hash-utils/hash.rb', line 400

def asort(&block)
    if block.nil?
       block =  Proc::new { |a, b| a <=> b }
    end
    Hash[self.sort { |a, b| block.call(a[1], b[1]) }]
end

#asort!(&block) ⇒ Hash

Sorts hash according to values, replaces the original one.

Parameters:

  • block (Proc)

    comparing block (see similar for #sort method)

Returns:

  • (Hash)

    reversed hash

See Also:

Since:

  • 0.5.0



416
417
418
# File 'lib/hash-utils/hash.rb', line 416

def asort!(&block)
    self.replace(self.asort(&block))
end

#compactHash

Returns a copy of self with all nil elements removed.

Returns:

  • (Hash)

    new hash

Since:

  • 0.1.0



104
105
106
# File 'lib/hash-utils/hash.rb', line 104

def compact
    self.reject { |k, v| v.nil? }
end

#compact!Hash

Removes nil elements from the hash. Returns nil if no changes were made, otherwise returns self.

Returns:

  • (Hash)

    new hash

Since:

  • 0.1.0



116
117
118
# File 'lib/hash-utils/hash.rb', line 116

def compact!
    self.reject! { |k, v| v.nil? }
end

#has_all?(keys) ⇒ Boolean Also known as: has_keys?

Indicates, all of the keys are available in Hash.

Parameters:

  • keys (Array)

    objects for checking

Returns:

  • (Boolean)

    true if yes, false in otherwise

Since:

  • 0.9.0



453
454
455
456
457
458
459
460
461
# File 'lib/hash-utils/hash.rb', line 453

def has_all?(keys)
    keys.each do |key|
        if not self.has_key? key
            return false
        end
    end
    
    return true
end

#has_some?(keys) ⇒ Boolean

Indicates, some of the keys are available in Hash.

Parameters:

  • keys (Array)

    objects for checking

Returns:

  • (Boolean)

    true if yes, false in otherwise

Since:

  • 0.9.0



473
474
475
476
477
478
479
480
481
# File 'lib/hash-utils/hash.rb', line 473

def has_some?(keys)
    keys.each do |key|
        if self.has_key? key
            return true
        end
    end
    
    return false
end

#keys_to_symHash

Converts all keys to symbols.

Returns:

  • (Hash)

    new hash

Since:

  • 0.1.0



228
229
230
# File 'lib/hash-utils/hash.rb', line 228

def keys_to_sym
    self.map_keys { |k| k.to_sym }
end

#keys_to_sym!Hash

Emulates #keys_to_sym on place. In fact, replaces old hash by new one.

Returns:

  • (Hash)

    new hash

Since:

  • 0.1.0



240
241
242
# File 'lib/hash-utils/hash.rb', line 240

def keys_to_sym!
    self.replace(self.keys_to_sym)
end

#ksort(&block) ⇒ Hash

Sorts hash according to keys. It’s equivalent of PHP ksort().

Be warn, this method have sense in Ruby 1.9 only. Ruby 1.8 doesn’t maintain pair order in Hashes.

Parameters:

  • block (Proc)

    comparing block (see similar for #sort method)

Returns:

  • (Hash)

    new sorted hash

See Also:

Since:

  • 0.5.0



366
367
368
369
370
371
# File 'lib/hash-utils/hash.rb', line 366

def ksort(&block)
    if block.nil?
       block =  Proc::new { |a, b| a <=> b }
    end
    Hash[self.sort { |a, b| block.call(a[0], b[0]) }]
end

#ksort!(&block) ⇒ Hash

Sorts hash according to keys, replaces the original one.

Parameters:

  • block (Proc)

    comparing block (see similar for #sort method)

Returns:

  • (Hash)

    new sorted hash

See Also:

Since:

  • 0.5.0



382
383
384
# File 'lib/hash-utils/hash.rb', line 382

def ksort!(&block)
    self.replace(self.ksort(&block))
end

#map_keys(&block) ⇒ Hash Also known as: collect_keys

Returns a new hash with the results of running block once for every key in self.

Parameters:

  • block (Proc)

    evaluating block

Returns:

  • (Hash)

    new hash

Since:

  • 0.1.0



166
167
168
169
170
# File 'lib/hash-utils/hash.rb', line 166

def map_keys(&block)
    self.map_pairs do |k, v|
        [block.call(k), v]
    end
end

#map_keys!(&block) ⇒ Hash Also known as: collect_keys!

Emulates #map_keys on place. In fact, replaces old hash by new one.

Parameters:

  • block (Proc)

    evaluating block

Returns:

  • (Hash)

    new hash

Since:

  • 0.1.0



183
184
185
# File 'lib/hash-utils/hash.rb', line 183

def map_keys!(&block)
    self.replace(self.map_keys(&block))
end

#map_pairs(&block) ⇒ Hash Also known as: collect_pairs

Returns a new hash with the results of running block once for every pair in self.

Parameters:

  • block (Proc)

    evaluating block

Returns:

  • (Hash)

    new hash

Since:

  • 0.1.0



129
130
131
132
133
134
135
136
137
138
# File 'lib/hash-utils/hash.rb', line 129

def map_pairs(&block)
    new = self.recreate
    
    self.each_pair do |k, v|
        new_k, new_v = block.call(k, v)
        new[new_k] = new_v
    end
    
    return new
end

#map_pairs!(&block) ⇒ Hash Also known as: collect_pairs!

Emulates #map_pairs on place. In fact, replaces old hash by new one.

Parameters:

  • block (Proc)

    evaluating block

Returns:

  • (Hash)

    new hash

Since:

  • 0.1.0



151
152
153
# File 'lib/hash-utils/hash.rb', line 151

def map_pairs!(&block)
    self.replace(self.map_pairs(&block))
end

#map_values(&block) ⇒ Hash Also known as: collect_values

Returns a new hash with the results of running block once for every value in self.

Parameters:

  • block (Proc)

    evaluating block

Returns:

  • (Hash)

    new hash

Since:

  • 0.11.0



198
199
200
201
202
# File 'lib/hash-utils/hash.rb', line 198

def map_values(&block)
    self.map_pairs do |k, v|
        [k, block.call(v)]
    end
end

#map_values!(&block) ⇒ Hash Also known as: collect_values!

Emulates #map_values on place. In fact, replaces old hash by new one.

Parameters:

  • block (Proc)

    evaluating block

Returns:

  • (Hash)

    new hash

Since:

  • 0.11.0



215
216
217
# File 'lib/hash-utils/hash.rb', line 215

def map_values!(&block)
    self.replace(self.map_values(&block))
end

#recreateHash

Recreates the hash, so creates empty one and assigns  the same default values.

Returns:

  • (Hash)

    new hash

Since:

  • 0.3.0



54
55
56
# File 'lib/hash-utils/hash.rb', line 54

def recreate
    self.class::create(self.default, &self.default_proc)
end

#recreate!Hash

Recreates the hash in place, so creates empty one, assigns  the same default values and replaces the old one.

Returns:

  • (Hash)

    new hash

Since:

  • 0.3.0



66
67
68
# File 'lib/hash-utils/hash.rb', line 66

def recreate!
    self.replace(self.recreate)
end

#remove!(&block) ⇒ Hash

Moves selected pairs outside the hash, so returns them. Output hash has the same default settings.

Parameters:

  • block (Proc)

    selecting block

Returns:

  • (Hash)

    removed selected pairs

Since:

  • 0.3.0



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/hash-utils/hash.rb', line 79

def remove!(&block)
    result = self.recreate
    delete = [ ]
    
    self.each_pair do |k, v|
        if block.call(k, v)
            result[k] = v
            delete << k
        end
    end
    
    delete.each do |k|
        self.delete(k)
    end
    
    return result
end

#reverseHash

Reverses order of the hash pairs.

Be warn, this method have sense in Ruby 1.9 only. Ruby 1.8 doesn’t maintain pair order in Hashes.

Returns:

  • (Hash)

    reversed hash

Since:

  • 0.5.0



430
431
432
# File 'lib/hash-utils/hash.rb', line 430

def reverse
    Hash[self.to_a.reverse]
end

#reverse!Hash

Reverses order of the hash pairs, replaces the original one.

Returns:

  • (Hash)

    reversed hash

Since:

  • 0.5.0



441
442
443
# File 'lib/hash-utils/hash.rb', line 441

def reverse!
    self.replace(self.reverse)
end

#some?(&block) ⇒ Boolean

Checks, at least one element value follows condition expressed in block. Block must return boolean.

Parameters:

  • block (Proc)

    checking block

Returns:

  • (Boolean)

    true if yes, false in otherwise

Since:

  • 0.2.0



315
316
317
318
319
# File 'lib/hash-utils/hash.rb', line 315

def some?(&block)
    self.one? do |pair|
        block.call(pair[1])
    end
end

#sort!(&block) ⇒ Hash

Simulates sorting in place. In fact, replaces old one by new one.

Parameters:

  • block (Proc)

    comparing block (see similar for #sort method)

Returns:

  • (Hash)

    new sorted hash

Since:

  • 0.5.0



349
350
351
# File 'lib/hash-utils/hash.rb', line 349

def sort!(&block)
    self.replace(Hash[self.sort(&block)])
end