Class: String
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
-
#to_query_hash ⇒ Object
convert an URI query string into a symbol hash, does not support array query records (yet).
- #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 |
#to_query_hash ⇒ Object
convert an URI query string into a symbol hash, does not support array query records (yet)
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/creative_rails_utilities/string.rb', line 31 def to_query_hash raise ArgumentError.new("Looks like unsupported array query is used, sorry!") if self.to_s[/\[\]\=/].present? query_hash = split("&").reduce({}) do |mem, i| key = i.split("=").first value = i.split("=").second mem[key.to_s] = value.to_s mem end return query_hash end |
#unindent ⇒ Object
19 20 21 |
# File 'lib/creative_rails_utilities/string.rb', line 19 def unindent gsub(/^#{self[/\A\s*/]}/, '') end |