5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/ioughta.rb', line 5
def self.included(base)
class << base
def ioughta_const(*data, &block)
each_resolved_ipair(data, block) do |nom, val|
const_set(nom, val)
end
end
alias_method :iota_const, :ioughta_const
def ioughta_hash(*data, &block)
each_resolved_ipair(data, block).to_h
end
alias_method :iota_hash, :ioughta_hash
private
DEFAULT_LAMBDA = proc(&:itself)
SKIP_SYMBOL = :_
def each_ipair_with_index(data, block = nil)
data = data.to_a.flatten(1)
lam = (data.shift if data[0].respond_to?(:call)) || block || DEFAULT_LAMBDA
data.each_with_index do |nom, i, j = i.succ|
yield nom, data[j].respond_to?(:call) ? lam = data.slice!(j) : lam, i
end
end
def each_resolved_ipair(*args)
return enum_for(__method__, *args) unless block_given?
each_ipair_with_index(*args) do |nom, lam, iota|
yield nom, lam.call(*[iota, nom].take(lam.arity.abs)) unless nom == SKIP_SYMBOL
end
end
end
end
|