Class: String

Inherits:
Object show all
Defined in:
lib/aromat/pad.rb,
lib/aromat/nstr.rb

Overview

Monkey-patch String Class

Instance Method Summary collapse

Instance Method Details

#lpad(size, fill = ' ') ⇒ String

Left-Pad: Left-Pads a String to a given length using a given filler.

Parameters:

  • size (Fixnum)
  • fill (String) (defaults to: ' ')

Returns:

  • (String)

    A copy of the original String, padded to [size] with [fill]



23
24
25
26
27
# File 'lib/aromat/pad.rb', line 23

def lpad size, fill = ' '
  s = clone
  s = "#{fill}#{s}" while s.size < size
  s.slice s.size - size, size
end

#nstrString

Non-Empty String: Eliminate Empty Strings.

Returns:

  • (String)

    The string if non-empty, nil otherwise



10
11
12
# File 'lib/aromat/nstr.rb', line 10

def nstr
  self == '' ? nil : self
end

#rpad(size, fill = ' ') ⇒ String

Right-Pad: Right-Pads a String to a given length using a given filler.

Parameters:

  • size (Fixnum)
  • fill (String) (defaults to: ' ')

Returns:

  • (String)

    A copy of the original String, padded to [size] with [fill]



12
13
14
15
16
# File 'lib/aromat/pad.rb', line 12

def rpad size, fill = ' '
  s = clone
  s = "#{s}#{fill}" while s.size < size
  s.slice 0, size
end