Class: Hash

Inherits:
Object show all
Defined in:
lib/zucker/hash.rb,
lib/zucker/aliases.rb,
lib/zucker/hash_to_proc.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.zip(keys, values) ⇒ Object



5
6
7
# File 'lib/zucker/hash.rb', line 5

def self.zip(keys,values)
  Hash[ *keys.zip( values ).flatten ]
end

Instance Method Details

#&(other) ⇒ Object



20
21
22
23
24
# File 'lib/zucker/hash.rb', line 20

def &(other)
  Hash[ *select{ |k,v|
    other[k] == v
  }.flatten ]
end

#<<(other) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/zucker/hash.rb', line 9

def <<(other)
  case
  when other.is_a?(Hash)
    merge! other
  when other.is_a?(Enumerable) || other.respond_to?(:to_splat)
    merge! Hash[*other]
  else
    raise TypeError, 'can only append other Hashs and Enumerables (or Classes that implement to_splat)'
  end
end

#to_procObject



5
6
7
8
9
10
11
12
13
# File 'lib/zucker/hash_to_proc.rb', line 5

def to_proc
  Proc.new{ |obj|
    if self.member? obj
      self[obj].to_proc.call obj
    else
      obj
    end
  }
end