Class: Array

Inherits:
Object show all
Defined in:
lib/muflax/array.rb,
lib/muflax/align.rb,
lib/muflax/blank.rb,
lib/muflax/deep_dup.rb

Overview

Copyright Steffie Dorn <[email protected]>, 2013 License: GNU GPL 3 <www.gnu.org/copyleft/gpl.html>

Instance Method Summary collapse

Instance Method Details

#align(str = "\t", alignment: :left, force: false, ansi: false, is_split: false) ⇒ Object



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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/muflax/align.rb', line 40

def align str="\t", alignment: :left, force: false, ansi: false, is_split: false
  just_function = case alignment
                  when :left  	; Proc.new{|e,l,_c| e.ljust 	l	}
                  when :right 	; Proc.new{|e,l,_c| e.rjust 	l	}
                  when :center	; Proc.new{|e,l,_c| e.center	l	}
                  when Proc   	; alignment
                  else
                    raise "invalid alignment: #{alignment}"
                  end

  # split all lines
  lines  	= []
  columns	= 0
  rx     	= force ? /[ ]*#{str}/ : str

  self.each do |line|
    line   	= line.split(rx, -1) unless is_split
    lines  	<< line
    columns	= [columns, line.size].max
  end

  columns -= 1 # very last column is always un-aligned

  columns.times.map do |column|
    # calculate column width
    wants    	= []
    new_block	= false
    width    	= 0
    first    	= 0
    max_cols 	= column + 1

    lines.each.with_index do |line, cur|
      if new_block
        new_block	= false
        first    	= cur
        width    	= 0
      end

      # treat last column and missing columns as useless
      w = line.size == max_cols ? nil : line[column]&.size

      if w.nil? # block done
        wants    	<< [first, cur, width] unless width == 0
        new_block	= true
      else
        width = [width, w].max
      end
    end
    wants << [first, lines.size, width] unless new_block or width == 0 # last block

    # justify column
    wants.each do |from, to, width|
      lines[from...to].each do |line|
        if elem = line[column]
          # take into account how much the element is internally longer than it appears
          elem_diff   	= ansi ? elem.to_s.length - elem.str_length : 0
          line[column]	= just_function.call(elem, width + elem_diff, column)
        end
      end
    end
  end

  lines.map{|l| l.join(str)}
end

#deep_dupObject



13
14
15
# File 'lib/muflax/deep_dup.rb', line 13

def deep_dup
  map(&:deep_dup)
end

#fifthObject



22
# File 'lib/muflax/array.rb', line 22

def fifth 	; self[4]	; end

#fourthObject



21
# File 'lib/muflax/array.rb', line 21

def fourth	; self[3]	; end

#rfind(*arg, &block) ⇒ Object



24
25
26
27
# File 'lib/muflax/array.rb', line 24

def rfind *arg, &block
  i = self.rindex(*arg, &block)
  i.nil? ? nil : self[i]
end

#secondObject

silly accessors



19
# File 'lib/muflax/array.rb', line 19

def second	; self[1]	; end

#thirdObject



20
# File 'lib/muflax/array.rb', line 20

def third 	; self[2]	; end

#triangleObject



7
8
9
10
11
12
13
14
15
16
# File 'lib/muflax/array.rb', line 7

def triangle
  return to_enum(:triangle) unless block_given?

  self.each.with_index do |a, ai|
    self.each.with_index do |b, bi|
      next if bi < ai
      yield [a, b]
    end
  end
end