Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/core_ext/string.rb
Instance Method Summary collapse
-
#blank_to_nil ⇒ String?
Convert blank strings to
nil
. -
#classify ⇒ String
Convert (underscored) file name to (camelcased) class name.
-
#cleanup ⇒ String
Fix messy oddities such as the use of two apostrophes instead of a quote.
-
#compact ⇒ String
Strip and collapse unnecessary whitespace.
-
#extract(pattern) ⇒ Object
Similar to
scan
, but remove matches from the string. -
#first_match(*patterns, default: nil) ⇒ String?
Apply the patterns in the given order and return…
-
#full_strip ⇒ Object
Similar to
strip
, but remove any leading or trailing non-letters/numbers which includes whitespace. -
#sectionize ⇒ String
Convert (camelcased) class name to (underscored) file name.
-
#strip_markup ⇒ String
Remove all XML/HTML tags and entities from the string.
-
#to_digest ⇒ String
Builds the MD5 hash as hex and returns the first eight characters.
-
#to_ff ⇒ Float
Same as
to_f
but accept both dot and comma as decimal separator.
Instance Method Details
#blank_to_nil ⇒ String?
Convert blank strings to nil
59 60 61 |
# File 'lib/core_ext/string.rb', line 59 def blank_to_nil self if present? end |
#classify ⇒ String
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.
21 22 23 |
# File 'lib/core_ext/string.rb', line 21 def classify split('/').last.remove(/\W/).camelcase end |
#cleanup ⇒ String
Fix messy oddities such as the use of two apostrophes instead of a quote
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 |
#compact ⇒ String
While similar to String#squish from ActiveSupport, newlines \n
are preserved and not collapsed into one space.
Strip and collapse unnecessary whitespace
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 adefault
is set -
nil
- if no pattern matches and nodefault
is set
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_strip ⇒ Object
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 |
#sectionize ⇒ String
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.
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_markup ⇒ String
Remove all XML/HTML tags and entities from the string
132 133 134 |
# File 'lib/core_ext/string.rb', line 132 def strip_markup self.gsub(/<.*?>|&[#\da-z]+;/i, '') end |
#to_digest ⇒ String
Builds the MD5 hash as hex and returns the first eight characters.
142 143 144 |
# File 'lib/core_ext/string.rb', line 142 def to_digest Digest::MD5.hexdigest(self)[0,8] end |
#to_ff ⇒ Float
Same as to_f
but accept both dot and comma as decimal separator
154 155 156 |
# File 'lib/core_ext/string.rb', line 154 def to_ff sub(/,/, '.').to_f end |