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


12
13
14
# File 'lib/core_ext/string.rb', line 12

def blank_to_nil
  self if present?
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:


22
23
24
25
26
27
# File 'lib/core_ext/string.rb', line 22

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


38
39
40
# File 'lib/core_ext/string.rb', line 38

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


49
50
51
# File 'lib/core_ext/string.rb', line 49

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:


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

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


44
45
46
# File 'lib/core_ext/string.rb', line 44

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

#strip_markupObject

Remove all XML/HTML tags and entities from the string


80
81
82
# File 'lib/core_ext/string.rb', line 80

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


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

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