Class: String

Inherits:
Object show all
Defined in:
lib/merb-core/core_ext/string.rb,
lib/merb-core/vendor/facets/inflect.rb

Defined Under Namespace

Classes: InvalidPathConversion

Instance Method Summary collapse

Instance Method Details

#/(o) ⇒ Object

Parameters

o<String>

The path component to join with the string.

Returns

String

The original path concatenated with o.

Examples

"merb"/"core_ext" #=> "merb/core_ext"


62
63
64
# File 'lib/merb-core/core_ext/string.rb', line 62

def /(o)
  File.join(self, o.to_s)
end

#camel_caseObject

Returns

String

The string converted to camel case.

Examples

"foo_bar".camel_case #=> "FooBar"


30
31
32
# File 'lib/merb-core/core_ext/string.rb', line 30

def camel_case
  split('_').map{|e| e.capitalize}.join
end

#escape_regexpObject

Returns

String

The string with all regexp special characters escaped.

Examples

"\*?{}.".escape_regexp #=> "\\*\\?\\{\\}\\."


12
13
14
# File 'lib/merb-core/core_ext/string.rb', line 12

def escape_regexp
  Regexp.escape self
end

#pluralObject Also known as: pluralize



207
208
209
# File 'lib/merb-core/vendor/facets/inflect.rb', line 207

def plural
  Language::English::Inflect.plural(self)
end

#relative_path_from(other) ⇒ Object



66
67
68
# File 'lib/merb-core/core_ext/string.rb', line 66

def relative_path_from(other)
  Pathname.new(self).relative_path_from(Pathname.new(other)).to_s
end

#singularObject Also known as: singularize



203
204
205
# File 'lib/merb-core/vendor/facets/inflect.rb', line 203

def singular
  Language::English::Inflect.singular(self)
end

#snake_caseObject

Returns

String

The string converted to snake case.

Examples

"FooBar".snake_case #=> "foo_bar"


21
22
23
# File 'lib/merb-core/core_ext/string.rb', line 21

def snake_case
  gsub(/\B[A-Z]/, '_\&').downcase
end

#to_const_pathObject

Returns

String

The path that is associated with the constantized string, assuming a conventional structure.

Examples

"FooBar::Baz".to_const_path # => "foo_bar/baz"


50
51
52
# File 'lib/merb-core/core_ext/string.rb', line 50

def to_const_path
  snake_case.gsub(/::/, "/")
end

#to_const_stringObject

Returns

String

The path string converted to a constant name.

Examples

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


39
40
41
# File 'lib/merb-core/core_ext/string.rb', line 39

def to_const_string
  gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
end