Class: String

Inherits:
Object show all
Defined in:
lib/utilities/string.rb

Overview

Waves extends String with a variety of methods for changing from singular to plural and back, and switching to different types of case and word separators. These methods are similar to those found in Rails and other frameworks, but some (all?) of the names are different. The names here were chosen for increased clarity and are hopefully easy to adjust to …

Instance Method Summary collapse

Instance Method Details

#/(string) ⇒ Object

Does a File.join on the two arguments joined by the /. Very handy for doing platform-safe paths without having to use File.join.

I unfortunately don’t recall where i first saw this … see Symbol extension as well, allowing for :files / ‘afilename.txt’



11
12
13
# File 'lib/utilities/string.rb', line 11

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

#camel_caseObject



31
32
33
# File 'lib/utilities/string.rb', line 31

def camel_case
  lower_camel_case.gsub(/^([a-z])/) { $1.upcase }
end

#lower_camel_caseObject



27
28
29
# File 'lib/utilities/string.rb', line 27

def lower_camel_case
  gsub(/(_)(\w)/) { $2.upcase }
end

#pluralObject Also known as: pluralize



21
22
23
# File 'lib/utilities/string.rb', line 21

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

#singularObject Also known as: singularize



15
16
17
# File 'lib/utilities/string.rb', line 15

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

#snake_caseObject



35
36
37
# File 'lib/utilities/string.rb', line 35

def snake_case
  gsub(/([a-z\d])([A-Z])/){ "#{$1}_#{$2}"}.tr("-", "_").downcase
end

#textObject



43
44
45
# File 'lib/utilities/string.rb', line 43

def text
  gsub(/[\_\-\.\:]/,' ')
end

#title_caseObject



39
40
41
# File 'lib/utilities/string.rb', line 39

def title_case
  gsub(/(^|\s)\s*([a-z])/) { $1 + $2.upcase }
end