Class: String
Instance Method Summary collapse
-
#blank? ⇒ Boolean
A string is blank if it’s empty or contains whitespaces only:.
- #camelize ⇒ Object
- #ensure_ends_with(fragment) ⇒ Object
- #ensure_starts_with(fragment) ⇒ Object
-
#margin ⇒ String
Remove whitespace margin.
- #underscore ⇒ Object
Instance Method Details
#blank? ⇒ Boolean
A string is blank if it’s empty or contains whitespaces only:
"".blank? # => true
" ".blank? # => true
" something here ".blank? # => false
118 119 120 |
# File 'lib/ruby/blank.rb', line 118 def blank? self !~ /\S/ end |
#camelize ⇒ Object
11 12 13 14 |
# File 'lib/ruby/string.rb', line 11 def camelize self.gsub(/\/(.?)/) { "::" + $1.upcase }. gsub(/(^|_|-)(.)/) { $2.upcase } end |
#ensure_ends_with(fragment) ⇒ Object
16 17 18 |
# File 'lib/ruby/string.rb', line 16 def ensure_ends_with(fragment) end_with?(fragment) ? self.dup : self + fragment end |
#ensure_starts_with(fragment) ⇒ Object
20 21 22 |
# File 'lib/ruby/string.rb', line 20 def ensure_starts_with(fragment) start_with?(fragment) ? self.dup : fragment + self end |
#margin ⇒ String
Remove whitespace margin.
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ruby/string.rb', line 30 def margin lines = self.dup.split($/) min_margin = 0 lines.each do |line| if line =~ /^(\s+)/ && (min_margin == 0 || $1.size < min_margin) min_margin = $1.size end end lines.map { |line| line.sub(/^\s{#{min_margin}}/, '') }.join($/) end |
#underscore ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/ruby/string.rb', line 3 def underscore self.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |