Class: String

Child modules and classes

Class String::InvalidPathConversion

Public Instance Methods


/ (o)

Concatenates a path

  $ "merb"/"core_ext" #=> "merb/core_ext"
    # File lib/merb/core_ext/string.rb, line 42
42:   def /(o)
43:     File.join(self, o.to_s)
44:   end

camel_case ()

"foo_bar".camel_case #=> "FooBar"

    # File lib/merb/core_ext/string.rb, line 19
19:   def camel_case
20:     split('_').map{|e| e.capitalize}.join
21:   end

escape_regexp ()

Escapes any characters in the string that would have special meaning in a regular expression.

  $ "\*?{}.".escape_regexp #=> "\\*\\?\\{\\}\\."
    # File lib/merb/core_ext/string.rb, line 9
 9:   def escape_regexp
10:     Regexp.escape self
11:   end

indent (indentation)

Takes lines of text, removes any indentation, and adds indentation number of spaces to each line

  $ "  Hello\n Bob\nHow are you?".indent(3)
  #=> "   Hello\n   Bob\n   How are you?"
    # File lib/merb/core_ext/string.rb, line 50
50:   def indent(indentation)
51:     match(/\s*/) && gsub(/^\s{0,#{$&.length}}/, " " * indentation)
52:   end

snake_case ()

"FooBar".snake_case #=> "foo_bar"

    # File lib/merb/core_ext/string.rb, line 14
14:   def snake_case
15:     gsub(/\B[A-Z]/, '_\&').downcase
16:   end

to_const_string ()

"merb/core_ext/string" #=> "Merb::CoreExt::String"

About 50% faster than string.split(’/’).map{ |s| s.camel_case }.join(’::’)

    # File lib/merb/core_ext/string.rb, line 26
26:   def to_const_string
27:     new_string = ""
28:     input = StringScanner.new(self.downcase)
29:     until input.eos?
30:       if input.scan(/([a-z][a-zA-Z\d]*)(_|$|\/)/)
31:         new_string << input[1].capitalize
32:         new_string << "::" if input[2] == '/'
33:       else
34:         raise InvalidPathConversion, self
35:       end
36:     end
37:     new_string
38:   end