Class: String
- Defined in:
- lib/vendor/rails_blank.rb,
lib/a_little_less/core_ext.rb
Constant Summary collapse
- BLANK_RE =
/\A[[:space:]]*\z/
Instance Method Summary collapse
-
#blank? ⇒ true, false
A string is blank if it’s empty or contains whitespaces only:.
- #camelize ⇒ Object
- #decamelize ⇒ Object
- #light_parameterize ⇒ Object
- #remove_extension ⇒ Object
- #safe_utf8 ⇒ Object
- #urlparams_to_hash ⇒ Object
Instance Method Details
#blank? ⇒ true, false
A string is blank if it’s empty or contains whitespaces only:
''.blank? # => true
' '.blank? # => true
"\t\n\r".blank? # => true
' blah '.blank? # => false
Unicode whitespace is supported:
"\u00a0".blank? # => true
118 119 120 |
# File 'lib/vendor/rails_blank.rb', line 118 def blank? BLANK_RE === self end |
#camelize ⇒ Object
17 18 19 |
# File 'lib/a_little_less/core_ext.rb', line 17 def camelize split('_').map(&:capitalize).join end |
#decamelize ⇒ Object
14 15 16 |
# File 'lib/a_little_less/core_ext.rb', line 14 def decamelize scan(/[A-Z][a-z]*/).join("_").downcase end |
#light_parameterize ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/a_little_less/core_ext.rb', line 20 def light_parameterize I18n.transliterate(self) .downcase .gsub(/[^a-z0-9]+/, '-') .gsub(/-+/, '-') .gsub(/^-|-$/, '') rescue => e logi "#{e.message} for '#{self}'" logi e.backtrace.join("\n") if e.backtrace self end |
#remove_extension ⇒ Object
48 49 50 |
# File 'lib/a_little_less/core_ext.rb', line 48 def remove_extension self.sub /\..*$/, '' end |
#safe_utf8 ⇒ Object
31 32 33 |
# File 'lib/a_little_less/core_ext.rb', line 31 def safe_utf8 self.tidy_bytes.split(//u).join.force_encoding 'UTF-8' end |
#urlparams_to_hash ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/a_little_less/core_ext.rb', line 34 def urlparams_to_hash hash = {} return hash if blank? split('&').each do |i| k, v = i.split('=') if k and v hash[ k ] = v end end hash end |