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 …

Notably, the inflector code here is not as comprehensive as the Rails code. This will be fixed in a future version of Waves.

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’



13
14
15
# File 'lib/utilities/string.rb', line 13

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

#camel_caseObject



25
26
27
# File 'lib/utilities/string.rb', line 25

def camel_case
	gsub(/(_)(\w)/) { $2.upcase }.gsub(/^([a-z])/) { $1.upcase }
end

#pluralObject



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

def plural
	gsub(/(s|sh|ch)$/,'\1es').gsub(/(i|y)$/,'ies').gsub(/([^s])$/,'\1s')
end

#singularObject



17
18
19
# File 'lib/utilities/string.rb', line 17

def singular
	gsub(/ies$/,'y').gsub(/es$/,'').gsub(/s$/,'')
end

#snake_caseObject



29
30
31
# File 'lib/utilities/string.rb', line 29

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

#textObject



37
38
39
# File 'lib/utilities/string.rb', line 37

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

#title_caseObject



33
34
35
# File 'lib/utilities/string.rb', line 33

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