Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/string.rb

Instance Method Summary collapse

Instance Method Details

#blank_to_nilString?

Convert blank strings to nil

Examples:

"foobar".blank_to_nil   # => "foobar"
" ".blank_to_nil        # => nil
"".blank_to_nil         # => nil
nil.blank_to_nil        # => nil

Returns:

  • (String, nil)

    converted string



59
60
61
# File 'lib/core_ext/string.rb', line 59

def blank_to_nil
  self if present?
end

#classifyString

Convert (underscored) file name to (camelcased) class name

Similar to classify from ActiveSupport, however, with a few differences:

  • Namespaces are ignored.

  • Plural strings are not singularized.

  • Characters other than A-Z, a-z, 0-9 and _ are removed.

Use sectionize to reverse this method.

Examples:

"navigational_aids".classify     # => "NavigationalAids"
"ENR".classify                   # => "ENR"
"ENR-4.1".classify               # => "ENR41"
"AIPP/LF/AIP/ENR-4.1".classify   # => "ENR41"

Returns:

  • (String)

    converted string



21
22
23
# File 'lib/core_ext/string.rb', line 21

def classify
  split('/').last.remove(/\W/).camelcase
end

#cleanupString

Fix messy oddities such as the use of two apostrophes instead of a quote

Examples:

"the ''Terror'' was a fine ship".cleanup   # => "the \"Terror\" was a fine ship"

Returns:



69
70
71
72
73
74
# File 'lib/core_ext/string.rb', line 69

def cleanup
  gsub(/[#{AIXM::MIN}]{2}|[#{AIXM::SEC}]/, '"').   # unify quotes
    gsub(/[#{AIXM::MIN}]/, "'").   # unify apostrophes
    gsub(/"[[:blank:]]*(.*?)[[:blank:]]*"/m, '"\1"').   # remove whitespace within quotes
    split(/\r?\n/).map { _1.strip.blank_to_nil }.compact.join("\n")   # remove blank lines
end

#compactString

Note:

While similar to String#squish from ActiveSupport, newlines \n are preserved and not collapsed into one space.

Strip and collapse unnecessary whitespace

Examples:

"  foo\n\nbar \r".compact   # => "foo\nbar"

Returns:

  • (String)

    compacted string



85
86
87
# File 'lib/core_ext/string.rb', line 85

def compact
  split("\n").map { _1.squish.blank_to_nil }.compact.join("\n")
end

#extract(pattern) ⇒ Object

Similar to scan, but remove matches from the string



96
97
98
# File 'lib/core_ext/string.rb', line 96

def extract(pattern)
  scan(pattern).tap { remove! pattern }
end

#first_match(*patterns, default: nil) ⇒ String?

Apply the patterns in the given order and return…

  • first capture group - if a pattern matches and contains a capture group

  • entire match - if a pattern matches and contains no capture group

  • default - if no pattern matches and a default is set

  • nil - if no pattern matches and no default is set

Examples:

"A/A: 123.5 mhz".first_match(/123\.5/)                   # => "123.5"
"A/A: 123.5 mhz".first_match(/:\s+([\d.]+)/)             # => "123.5"
"A/A: 123.5 mhz".first_match(/121\.5/)                   # nil
"A/A: 123.5 mhz".first_match(/(121\.5)/)                 # nil
"A/A: 123.5 mhz".first_match(/121\.5/, default: "123")   # "123"

Parameters:

  • patterns (Array<Regexp>)

    one or more patterns to apply in order

  • default (String) (defaults to: nil)

    string to return instead of nil if the pattern doesn’t match

Returns:



117
118
119
120
121
122
123
124
# File 'lib/core_ext/string.rb', line 117

def first_match(*patterns, default: nil)
  patterns.each do |pattern|
    if captures = match(pattern)
      return captures[1] || captures[0]
    end
  end
  default
end

#full_stripObject

Similar to strip, but remove any leading or trailing non-letters/numbers which includes whitespace



91
92
93
# File 'lib/core_ext/string.rb', line 91

def full_strip
  remove(/\A[^\p{L}\p{N}]*|[^\p{L}\p{N}]*\z/)
end

#sectionizeString

Convert (camelcased) class name to (underscored) file name

Similar to underscore from ActiveSupport, however, with a few differences:

  • Namespaces are ignored.

  • AIP naming conventions are honored.

Use classify to reverse this method.

Examples:

"NavigationalAids".sectionize       # => "navigational_aids"
"ENR".sectionize                    # => "ENR"
"ENR41".sectionize                  # => "ENR-4.1"
"AIPP::LF::AIP::ENR41".sectionize   # => "ENR-4.1"

Returns:

  • (String)

    converted string



41
42
43
44
45
46
47
48
# File 'lib/core_ext/string.rb', line 41

def sectionize
  case klass = self.split('::').last
    when /\A([A-Z]{2,3})\z/ then $1
    when /\A([A-Z]{2,3})(\d)\z/ then "#{$1}-#{$2}"
    when /\A([A-Z]{2,3})(\d)(\d+)\z/ then "#{$1}-#{$2}.#{$3}"
    else klass.underscore
  end
end

#strip_markupString

Remove all XML/HTML tags and entities from the string

Examples:

"this <em>is</em> a <br> test".strip_markup   # => "this is a  test"

Returns:



132
133
134
# File 'lib/core_ext/string.rb', line 132

def strip_markup
  self.gsub(/<.*?>|&[#\da-z]+;/i, '')
end

#to_digestString

Builds the MD5 hash as hex and returns the first eight characters.

Examples:

"this is a test".to_digest   # => "54b0c58c"

Returns:



142
143
144
# File 'lib/core_ext/string.rb', line 142

def to_digest
  Digest::MD5.hexdigest(self)[0,8]
end

#to_ffFloat

Same as to_f but accept both dot and comma as decimal separator

Examples:

"5.5".to_ff    # => 5.5
"5,6".to_ff    # => 5.6
"5,6".to_f     # => 5.0   (sic!)

Returns:

  • (Float)

    number parsed from text



154
155
156
# File 'lib/core_ext/string.rb', line 154

def to_ff
  sub(/,/, '.').to_f
end