Class: Array

Inherits:
Object show all
Defined in:
lib/innate/array.rb

Direct Known Subclasses

FileLines

Instance Method Summary collapse

Instance Method Details

#columns(width = 80, spaces = 2, spacer = ' ') ⇒ Object



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
# File 'lib/innate/array.rb', line 43

def columns(width = 80, spaces = 2, spacer = ' ')
  results = row_col(width, spaces, spacer) do |array, cols, empty_element|
    len = array.length
    col_len = len / cols
    if len > (cols * col_len)
      col_len += 1
      if col_len * cols - len >= col_len
        fail = true
        next :fail
      end
    end
    if len % col_len > 0
      array += ([empty_element] * (col_len - length % col_len))
    end
    col_data = []
    cols.times do |c|
      col_data << array.slice!(0, col_len)
    end
    #if col_data.last == [] # or col_data.last == nil
      ## shouldn't get here anymore...
      #:fail
    #else
      [col_data.transpose, col_data]
    #end
  end
  row_col_format(spaces, spacer, *results)
end

#match?(string) ⇒ Boolean

Returns:

  • (Boolean)


2
3
4
5
6
7
8
9
10
# File 'lib/innate/array.rb', line 2

def match? string
  find do |i|
    if i.is_a? String
      i == string
    elsif i.is_a? Regexp
      i =~ string
    end
  end
end

#pc(w = 130, s = 2, sr = ' ') ⇒ Object



71
72
73
# File 'lib/innate/array.rb', line 71

def pc(w = 130, s = 2, sr = ' ')
  puts sort.columns(w, s, sr)
end

#pr(w = 130, s = 2, sr = ' ') ⇒ Object



75
76
77
# File 'lib/innate/array.rb', line 75

def pr(w = 130, s = 2, sr = ' ')
  puts sort.rows(w, s, sr)
end

#rows(width = 80, spaces = 2, spacer = ' ') ⇒ Object



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
# File 'lib/innate/array.rb', line 12

def rows(width = 80, spaces = 2, spacer = ' ')
  results = row_col(width, spaces, spacer) do |array, cols, empty_element, max|
    r = []
    stat = :ok
    begin
      s = array.slice!(0, cols)
      if max
        break :fail if :fail == s.inject(0) do |a, b| 
          c = a + b
          if c > max
            stat = :fail
            break :fail
          else
            c
          end
        end
      end
      r << s
    end until array == []
    next :fail if stat == :fail
    l = r.last.length
    if l == 0
      r.pop
    elsif l < cols
      r.push(r.pop + [empty_element] * (cols - l))
    end
    [r, r.transpose]
  end
  row_col_format(spaces, spacer, *results)
end