Class: String

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

Overview

Monkey-patch String Class

Instance Method Summary collapse

Instance Method Details

#camelcaseString

Camel Case: Converts String to CamelCase.

Returns:

  • (String)

    The string converted to camel-case



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

def camelcase
  self.gsub(/(^|[_-])([a-zA-Z])/) { |s| s[/[a-zA-Z]$/].upcase }
end

#flatcase(delim = '') ⇒ String

Flat Case: Generic String converter for kebab-case / snake_case.

Parameters:

  • delim (String) (defaults to: '')

    Delimiter to be placed between parts

Returns:

  • (String)

    The string converted to flatcase.



31
32
33
# File 'lib/aromat/case.rb', line 31

def flatcase delim = ''
  self.gsub(/((^|[a-z])[A-Z])|([_-][a-zA-Z])/) { |s| (s.length > 1 ? "#{s[/^[a-z]/]}#{delim}" : '') + s[/[a-zA-Z]$/].downcase }
end

#kebabcaseString

Kebab Case: Converts String to kebab-case.

Returns:

  • (String)

    The string converted to kebab-case



17
18
19
# File 'lib/aromat/case.rb', line 17

def kebabcase
  flatcase '-'
end

#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

#snakecaseObject

Snake Case: Converts String to snake_case.



23
24
25
# File 'lib/aromat/case.rb', line 23

def snakecase
  flatcase '_'
end