module Columnize
Module to format an Array into a single string with embedded newlines, On printing the string, the columns are aligned.
Summary¶ ↑
Return a string from an array with embedded newlines formatted so that when printed the columns are aligned. See below for examples and options to the main method +columnize+.
License¶ ↑
Columnize is copyright (C) 2007-2011, 2013 Rocky Bernstein <rockyb@rubyforge.net>
All rights reserved. You can redistribute and/or modify it under the same terms as Ruby.
Also available in Python (columnize), and Perl (Array::Columnize)
Copyright (C) 2007-2011, 2013 Rocky Bernstein <rockyb@rubyforge.net>
Part of Columnize to format in either direction
Sets constant Columnize::VERSION, the version number of this package. It is used in Gem creation but can also be consulted after require'ing 'columnize'.
Constants
- DEFAULT_OPTS
When an option is not specified for the below keys, these are the defaults.
- ROOT_DIR
Pull in the rest of my pieces
- VERSION
The current version of this package
Attributes
Add columnize_opts
instance variable to classes that mix in
this module. The type should be a kind of hash in file
columnize/opts
.
Public Class Methods
Columnize.columize([args]) => 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 ', '
# File lib/columnize.rb, line 68 def self.columnize(*args) list = args.shift opts = parse_columnize_options(args) Columnizer.new(list, opts).columnize end
Adds #columnize_opts to the singleton level of included class
# 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
Options parsing routine for ::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
.
# 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
Public Instance Methods
# 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