Module: Columnize

Defined in:
lib/columnize.rb,
lib/columnize/opts.rb,
lib/columnize/version.rb,
lib/columnize/columnize.rb

Overview

Copyright © 2007-2011, 2013 Rocky Bernstein <[email protected]>

Part of Columnize to format in either direction

Defined Under Namespace

Classes: Columnizer

Constant Summary collapse

ROOT_DIR =

Pull in the rest of my pieces

File.dirname(__FILE__)
DEFAULT_OPTS =

When an option is not specified for the below keys, these are the defaults.

{:line_prefix => '', :displaywidth => 80}
VERSION =

The current version of this package

'0.8.9'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#columnize_optsObject

Add columnize_opts instance variable to classes that mix in this module. The type should be a kind of hash in file columnize/opts.



29
30
31
# File 'lib/columnize.rb', line 29

def columnize_opts
  @columnize_opts
end

Class Method Details

.columnize(*args) ⇒ Object

Columnize.columize() => String

Return a string from an array with embedded newlines formatted so
that when printed the columns are aligned.

For example, for a line width of 4 characters (arranged vertically):
    a = (1..4).to_a
    Columnize.columnize(a) => '1  3\n2  4\n'

Alternatively:
    a.columnize => '1  3\n2  4\n'

Arranged horizontally:
    a.columnize(:arrange_vertical => false) =>
      ['1', '2,', '3', '4'] => '1  2\n3  4\n'

Formatted as an array using format specifier '%02d':
    puts (1..10).to_a.columnize(:arrange_array => true, :colfmt => '%02d',
                                :displaywidth => 10) =>
    [01, 02,
     03, 04,
     05, 06,
     07, 08,
     09, 10,
    ]

Each column is only as wide as necessary. By default, columns are separated by two spaces. Options are available for setting

  • the line display width

  • a column separator

  • a line prefix

  • a line suffix

  • A format specify for formatting each item each array item to a string

  • whether to ignore terminal codes in text size calculation

  • whether to left justify text instead of right justify

  • whether to format as an array - with surrounding [] and separating ‘, ’



68
69
70
71
72
# File 'lib/columnize.rb', line 68

def self.columnize(*args)
  list = args.shift
  opts = parse_columnize_options(args)
  Columnizer.new(list, opts).columnize
end

.included(base) ⇒ Object

Adds columnize_opts to the singleton level of included class



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

def self.included(base)
  # screw class variables, we'll use an instance variable on the class singleton
  class << base
    attr_accessor :columnize_opts
  end
  base.columnize_opts = DEFAULT_OPTS.dup
end

.parse_columnize_options(args) ⇒ Object

Options parsing routine for Columnize::columnize. In the preferred newer style, args is a hash where each key is one of the option names.

In the older style positional arguments are used and the positions are in the order: displaywidth, colsep, arrange_vertical, ljust, and line_prefix.



26
27
28
29
30
31
32
33
34
# File 'lib/columnize/opts.rb', line 26

def self.parse_columnize_options(args)
  if 1 == args.size && args[0].kind_of?(Hash) # explicitly passed as a hash
    args[0]
  elsif !args.empty? # passed as ugly positional parameters.
    Hash[args.zip([:displaywidth, :colsep, :arrange_vertical, :ljust, :line_prefix]).map(&:reverse)]
  else
    {}
  end
end

Instance Method Details

#columnize(*args) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/columnize.rb', line 83

def columnize(*args)
  return Columnize.columnize(*args) if args.length > 1
  opts = args.empty? ? {} : args.pop
  @columnize_opts ||= self.class.columnize_opts.dup
  @columnizer ||= Columnizer.new(self, @columnize_opts)
  # make sure that any changes to list or opts get passed to columnizer
  @columnizer.list = self unless @columnizer.list == self
  @columnizer.opts = @columnize_opts.merge(opts) unless @columnizer.opts == @columnize_opts and opts.empty?
  @columnizer.columnize
end