Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/hashslice.rb

Constant Summary collapse

VERSION_HASHSLICE =

The version of the hashslice library

'1.1.0'

Instance Method Summary collapse

Instance Method Details

#[](*args) ⇒ Object Also known as: slice

Retrieve a hash slice. If a single key is provided, returns a single value. If multiple keys are provided, an array of values is returned.

Examples:

hash = {'a' => 1, 'b' => 2, 'c' => 3}
hash['a']       -> 1
hash['a', 'c']  -> [1, 3]


17
18
19
20
21
22
23
# File 'lib/hashslice.rb', line 17

def [](*args)
  if args.length == 1
    href(args[0])
  else
    args.map{ |k| href(k) }
  end
end

#[]=(*args) ⇒ Object

Hash slice assignment. You can assign a list of values to a list of keys in a single operation on a one for one basis.

If the number of keys exceeds the number of values, the remaining keys are assigned a value of nil.

If the number of values exceeds the number of keys, the extra values are dropped.

Examples:

hash['a'] = 1, 2          -> {a => [1, 2]}
hash['a', 'b'] = 3, 4     -> {a => 3, b => 4}
hash['a', 'b'] = 5        -> {a => 5, b => nil}
hash['a', 'b'] = 3, 4, 5  -> {a => 3, b => 4}


43
44
45
46
47
48
49
50
51
# File 'lib/hashslice.rb', line 43

def []=(*args)
  if args.length <= 2
    hset(*args)
  else
    values = args.pop # Last arg is the value. The rest are keys.
    values = [values] unless values.is_a?(Array)
    args.each_index{ |i| hset(args[i], values[i]) }
  end
end

#hash_of(*args) ⇒ Object

Returns a sub-hash of the current hash.

Example:

hash = {'a' => 1, 'b' => 2, 'c' => 3}
hash.hash_of('a', 'b') -> {'a' => 1, 'b' => 2}


60
61
62
63
64
# File 'lib/hashslice.rb', line 60

def hash_of(*args)
  temp = {}
  args.map{ |k| temp[k] = href(k) }
  temp
end

#hrefObject



2
# File 'lib/hashslice.rb', line 2

alias href []

#hsetObject



3
# File 'lib/hashslice.rb', line 3

alias hset []=