class Array
Copyright Freya Dorn <freya.siv.dorn@gmail.com>, 2013 License: GNU GPL 3 <www.gnu.org/copyleft/gpl.html>
Public Instance Methods
align(str="\t", alignment: :left, force: false, ansi: false, is_split: false)
click to toggle source
# 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_dup()
click to toggle source
# File lib/muflax/deep_dup.rb, line 13 def deep_dup map(&:deep_dup) end
fifth()
click to toggle source
# File lib/muflax/array.rb, line 22 def fifth ; self[4] ; end
fourth()
click to toggle source
# File lib/muflax/array.rb, line 21 def fourth ; self[3] ; end
rfind(*arg, &block)
click to toggle source
# File lib/muflax/array.rb, line 24 def rfind *arg, &block i = self.rindex(*arg, &block) i.nil? ? nil : self[i] end
second()
click to toggle source
silly accessors
# File lib/muflax/array.rb, line 19 def second ; self[1] ; end
third()
click to toggle source
# File lib/muflax/array.rb, line 20 def third ; self[2] ; end
triangle() { |a, b| ... }
click to toggle source
# 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