Module: StringHelper

Included in:
Worklog, WorklogCLI
Defined in:
worklog/string_helper.rb

Overview

Helpers for String manipulation

Instance Method Summary collapse

Instance Method Details

#format_left(string) ⇒ String

Format a string to be left-aligned in a fixed-width field

Parameters:

  • string (String)

    the string to format

Returns:

  • (String)

    the formatted string



32
33
34
# File 'worklog/string_helper.rb', line 32

def format_left(string)
  format('%18s', string)
end

#pluralize(count, singular, plural = nil) ⇒ String

Pluralize a word based on a count. If the plural form is irregular, it can be provided. Otherwise, it will be generated automatically.

Parameters:

  • count (Integer)

    the count to base the pluralization on

  • singular (String)

    the singular form of the word

  • plural (String) (defaults to: nil)

    the plural form of the word, if it is irregular. Otherwise it will be generated.

Returns:

  • (String)

    the pluralized word



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'worklog/string_helper.rb', line 12

def pluralize(count, singular, plural = nil)
  if count == 1
    singular
  else
    return plural if plural

    return "#{singular[0..-2]}ies" if singular.end_with? 'y'

    return "#{singular}es" if singular.end_with? 'ch', 's', 'sh', 'x', 'z'

    return "#{singular[0..-2]}ves" if singular.end_with? 'f', 'fe'

    "#{singular}s"
  end
end