Class: Array

Inherits:
Object
  • Object
show all
Includes:
ObjectRubyExtended
Defined in:
lib/ruby_extended/array.rb,
lib/ruby_extended/object.rb

Instance Method Summary collapse

Methods included from ObjectRubyExtended

included

Instance Method Details

#duplicates(options = {}) ⇒ Object



96
97
98
99
100
# File 'lib/ruby_extended/array.rb', line 96

def duplicates(options={})
  self.select {|e| self.count(e) > 1}.uniq.map do |v|
    options[:full] ? { item:  v, count: self.count(v) } : v
  end
end

#sample_index(count = 1, offset: nil) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/ruby_extended/array.rb', line 74

def sample_index(count=1, offset: nil)
  indexes = (0..(self.length - 1)).to_a
  indexes = indexes.slice(offset + 1, indexes.length) if offset
  return nil if indexes.nil?
  result = indexes.sample(count)
  result = result.first if count == 1
  result
end

#sort_lv(&block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ruby_extended/array.rb', line 5

def sort_lv(&block)
  lang = [
    %w[e ē], %w[u ū], %w[i ī], %w[a ā], %w[s š], %w[g ģ], %w[k ķ], %w[l ļ],
    %w[z ž], %w[c č], %w[n ņ], %w[E Ē], %w[U Ū], %w[I I], %w[A Ā], %w[S Š],
    %w[G Ģ], %w[K Ķ], %w[L Ļ], %w[Z Ž], %w[C Č], %w[N Ņ]
  ]

  self.sort_by do |item|
    value = (block_given? ? yield(item) : item)
    lang.each do |en, lv|
      value = UnicodeUtils.downcase(value).gsub(lv, en)
    end
    value
  end
end

#sum(o = {}) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ruby_extended/array.rb', line 103

def sum(o={})
  res, str = [], false

  self.map do |e|
    ce = o[:key] ? e[o[:key]] : e
    if o[:only_numbers]
      res << ce if ce.is_a?(Numeric)
    else
      str = true if ce.kind_of?(String)
      res << ce unless ce.nil?
    end
  end

  str ? res.join('') : res.reduce(:+)
end

#tabulate(num, o = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ruby_extended/array.rb', line 22

def tabulate(num, o = {})
  return nil  if num < 1
  return self if num == 1

  split_by  = o.has_key?(:split_by)  ? o[:split_by]  : :columns
  direction = o.has_key?(:direction) ? o[:direction] : :horizontal
  arr, res  = self, []
  i         = (arr.length / num.to_f).round

  if direction == :horizontal
    ii = 0

    if split_by == :columns
      rows  = ( (i == 1)   ? [0] : (0..i - 1).to_a   )
      cells = ( (num == 1) ? [0] : (0..num - 1).to_a )
    end

    if split_by == :rows
      rows  = ( (num == 1) ? [0] : (0..num - 1).to_a )
      cells = ( (i == 1)   ? [0] : (0..i - 1).to_a   )
    end

    rows.each do |row_i|
      cells.each do |cell_i|
        res[row_i]         = [] if res[row_i].nil?
        res[row_i][cell_i] = arr[ii]
        ii                 = ii + 1
      end
    end

    res
  end

  if direction == :vertical

    if split_by == :columns
      (0..(i - 1)).to_a.each do |r_i|
        res[r_i] = (r_i).step(arr.size * 5, i).to_a.slice(0, num).map {|ii| arr[ii]}
      end
    end

    if split_by == :rows
      (0..(num - 1)).to_a.each do |r_i|
        res[r_i] = (r_i).step(arr.length * 5, num).to_a.slice(0, i).map {|ii| arr[ii]}
      end
    end
  end

  res
end

#to_tree(o = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ruby_extended/array.rb', line 120

def to_tree(o = {})
  o[:id_key]                 = :id        unless o.has_key?(:id_key)
  o[:parent_id_key]          = :parent_id unless o.has_key?(:parent_id_key)
  o[:children_key]           = :children  unless o.has_key?(:children_key)
  o[:include_id]             = true       unless o.has_key?(:include_id)
  o[:include_parent_id]      = false      unless o.has_key?(:include_parent_id)
  o[:include_empty_children] = false      unless o.has_key?(:include_empty_children)
  o[:to_hash]                = false      unless o.has_key?(:to_hash)

  res = Array.to_tree_children_loop(nil, self, o)
  res = Array.to_tree_hash_loop(res, o) if o[:to_hash]
  res
end

#uniq_by_key(key) ⇒ Object



84
85
86
87
88
89
90
91
92
93
# File 'lib/ruby_extended/array.rb', line 84

def uniq_by_key(key)
  keys, res, found = [], [], false
  self.each do |item|
    found = true if item.has_key?(key)
    next if keys.include?(item[key])
    res << item
    keys << item[key]
  end
  found ? res : self
end