Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/quantify/core_extensions/string.rb
Instance Method Summary collapse
- #remove_underscores ⇒ Object
- #starts_with_number? ⇒ Boolean
- #starts_with_valid_unit_term? ⇒ Boolean
- #to_quantity ⇒ Object (also: #to_q)
- #with_superscript_characters ⇒ Object
- #without_superscript_characters ⇒ Object
- #word_count ⇒ Object
- #words ⇒ Object
Instance Method Details
#remove_underscores ⇒ Object
12 13 14 |
# File 'lib/quantify/core_extensions/string.rb', line 12 def remove_underscores self.gsub("_"," ") end |
#starts_with_number? ⇒ Boolean
24 25 26 |
# File 'lib/quantify/core_extensions/string.rb', line 24 def starts_with_number? (/\A#{Unit::NUMBER_REGEX}/i).match(self) ? true : false end |
#starts_with_valid_unit_term? ⇒ Boolean
28 29 30 31 32 33 34 35 36 |
# File 'lib/quantify/core_extensions/string.rb', line 28 def starts_with_valid_unit_term? return false unless term = /\A#{Unit.unit_label_regex}#{Unit::INDEX_REGEX}?/.match(self) || /\A#{Unit.unit_symbol_regex}#{Unit::INDEX_REGEX}?/.match(self) || /\A#{Unit.unit_name_regex}#{Unit::INDEX_REGEX}?/.match(self) || /\A#{Unit::UNIT_DENOMINATOR_REGEX}/.match(self) || /\A(#{Unit::UNIT_PREFIX_TERMS_REGEX}|#{Unit::UNIT_SUFFIX_TERMS_REGEX})/i.match(self) return term[0] end |
#to_quantity ⇒ Object Also known as: to_q
38 39 40 |
# File 'lib/quantify/core_extensions/string.rb', line 38 def to_quantity Quantify::Quantity.parse(self) end |
#with_superscript_characters ⇒ Object
4 5 6 |
# File 'lib/quantify/core_extensions/string.rb', line 4 def with_superscript_characters self.gsub(/\^2\b/,"²").gsub(/\^3\b/,"³") end |
#without_superscript_characters ⇒ Object
8 9 10 |
# File 'lib/quantify/core_extensions/string.rb', line 8 def without_superscript_characters self.gsub(/¹\b/,"").gsub(/²\b/,"^2").gsub(/³\b/,"^3") end |
#word_count ⇒ Object
20 21 22 |
# File 'lib/quantify/core_extensions/string.rb', line 20 def word_count words.size end |
#words ⇒ Object
16 17 18 |
# File 'lib/quantify/core_extensions/string.rb', line 16 def words split(/\s+/) end |