Module: Ykutils::DataStructOp

Included in:
StructuredText, YamlOp
Defined in:
lib/ykutils/datastructop.rb

Instance Method Summary collapse

Instance Method Details

#exchange(target_ary, hash, item_key, index_key, content) ⇒ Object



3
4
5
6
7
8
9
10
# File 'lib/ykutils/datastructop.rb', line 3

def exchange(target_ary, hash, item_key, index_key, content)
  v = hash[item_key]
  if v
    target_ary[v[index_key]] = content
  else
    target_ary << content
  end
end

#make_array(ary, num, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ykutils/datastructop.rb', line 28

def make_array(ary, num, &block)
  ret_ary = []
  if block
    ary.each do |line|
      ary = block.call(line, num)
      ret_ary += ary if ary
      num += 1
    end
  else
    ret_ary = ary
  end

  ret_ary
end

#make_hash(ary, num, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ykutils/datastructop.rb', line 43

def make_hash(ary, num, &block)
  kv_ary = []
  if block
    ary.each do |line|
      ary = block.call(line, num)
      kv_ary += ary if ary
      num += 1
    end
  else
    kv_ary = ary.collect { |line| [line, nil] }.flatten
  end

  Hash[*kv_ary]
end

#select_array(ary, num, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ykutils/datastructop.rb', line 12

def select_array(ary, num, &block)
  ret_ary = []
  if block
    ary.each do |line|
      ret_ary << [line, num] if block.call(line, num)
      num += 1
    end
  else
    ary.each do |line|
      ret_ary << [line, num]
      num += 1
    end
  end
  ret_ary
end