Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/na/array.rb

Overview

Extensions to Ruby’s Array class for todo management and formatting.

Examples:

Remove bad elements from an array

['foo', '', nil, 0, false, 'bar'].remove_bad #=> ['foo', 'bar']

Direct Known Subclasses

NA::Actions

Instance Method Summary collapse

Instance Method Details

#remove_badArray

Like Array#compact – removes nil items, but also removes empty strings, zero or negative numbers and FalseClass items

Examples:

['foo', '', nil, 0, false, 'bar'].remove_bad #=> ['foo', 'bar']

Returns:

  • (Array)

    Array without “bad” elements



15
16
17
# File 'lib/na/array.rb', line 15

def remove_bad
  compact.map { |x| x.is_a?(String) ? x.strip : x }.select(&:good?)
end

#wrap(width, indent, color) ⇒ Array, String

Wrap each string in the array to the given width and indent, with color

Examples:

['foo', 'bar'].wrap(80, 2, '{g}') #=> "\n{g}  • foo{x}\n{g}  • bar{x}"

Parameters:

  • width (Integer)

    Maximum line width

  • indent (Integer)

    Indentation spaces

  • color (String)

    Color code to apply

Returns:



27
28
29
30
31
32
33
34
# File 'lib/na/array.rb', line 27

def wrap(width, indent, color)
  return map { |l| "#{color}  #{l.wrap(width, 2)}" } if width < 60

  map! do |l|
    "#{color}#{' ' * indent}• #{l.wrap(width, indent)}{x}"
  end
  "\n#{join("\n")}"
end