Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/creative_rails_utilities/string.rb
Constant Summary collapse
- Alpha26 =
("a".."z").to_a
Instance Method Summary collapse
- #clean_whitespace ⇒ Object
-
#to_bool ⇒ Object
Convert string to boolean.
- #to_i26 ⇒ Object
- #unindent ⇒ Object
Instance Method Details
#clean_whitespace ⇒ Object
15 16 17 |
# File 'lib/creative_rails_utilities/string.rb', line 15 def clean_whitespace return strip.gsub(/\s{2,}/, ' ') end |
#to_bool ⇒ Object
Convert string to boolean.
24 25 26 27 28 |
# File 'lib/creative_rails_utilities/string.rb', line 24 def to_bool return true if self[/\A(true)|(1)|(y(es)?)\z/i] return false if self[/\A(false)|(0)|(no?)|(nil)\z/i] || self == "" raise(ArgumentError.new "could not interpret '#{self}' as boolean.") end |
#to_i26 ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/creative_rails_utilities/string.rb', line 5 def to_i26 result = 0 downcase! (1..length).each do |i| char = self[-i] result += 26**(i-1) * (Alpha26.index(char) + 1) end result end |
#unindent ⇒ Object
19 20 21 |
# File 'lib/creative_rails_utilities/string.rb', line 19 def unindent gsub(/^#{self[/\A\s*/]}/, '') end |