Class: Array
Overview
refactorings
Direct Known Subclasses
Instance Method Summary collapse
- #columns(width = 80, spaces = 2, spacer = ' ') ⇒ Object
- #match?(string) ⇒ Boolean
- #pc(w = 130, s = 2, sr = ' ') ⇒ Object
- #pr(w = 130, s = 2, sr = ' ') ⇒ Object
- #rows(width = 80, spaces = 2, spacer = ' ') ⇒ Object
Instance Method Details
#columns(width = 80, spaces = 2, spacer = ' ') ⇒ Object
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 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/innate/array.rb', line 44 def columns(width = 80, spaces = 2, spacer = ' ') return '' if length == 0 best = nil bestcw = nil sorted = self # changed my mind: don't sort 1.upto(width) do |columns| space = spacer.length * spaces * (columns - 1) rows = [] cols = [] cw = [0] * columns col_len = length / columns col_len += 1 if length > (columns * col_len) temp = nil if length % col_len > 0 temp = sorted + ([''] * (col_len - length % col_len)) else temp = sorted end columns.times do |c| t = temp[(col_len * c)...(col_len * (c + 1))] cols << t end next if cols.last == [] or cols.last == nil cols = cols.transpose result = cols.each do |row| break :fail unless check_cols cw, rows, row, width, space, columns end if result != :fail best = rows bestcw = cw end break if col_len == 1 end fs = bestcw.collect {|w| "%#{w}-s" }.join(spacer * spaces) best.collect {|row| format(fs, *row) }.join "\n" end |
#match?(string) ⇒ 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
85 86 87 |
# File 'lib/innate/array.rb', line 85 def pc(w = 130, s = 2, sr = ' ') puts sort.columns(w, s, sr) end |
#pr(w = 130, s = 2, sr = ' ') ⇒ Object
89 90 91 |
# File 'lib/innate/array.rb', line 89 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 42 |
# File 'lib/innate/array.rb', line 12 def rows(width = 80, spaces = 2, spacer = ' ') return '' if length == 0 best = nil bestcw = nil sorted = self # changed my mind: don't sort 1.upto(width) do |columns| space = spacer.length * spaces * (columns - 1) break if columns + space > width t = [] rows = [] cw = [0] * columns result = sorted.each do |item| if t.length == columns break :fail unless check_cols cw, rows, t, width, space, columns t = [] end t << item.to_s end t += [''] * (columns - t.length) unless result == :fail result = :fail unless check_cols cw, rows, t, width, space, columns end if result != :fail best = rows bestcw = cw end end fs = bestcw.collect {|w| "%#{w}-s" }.join(spacer * spaces) best.collect {|row| format(fs, *row) }.join "\n" end |