Module: Waves::Utilities::String

Included in:
String
Defined in:
lib/utilities/string.rb

Overview

Utility methods mixed into String.

Instance Method Summary collapse

Instance Method Details

#/(string) ⇒ Object

Syntactic sugar for using File.join to concatenate the argument to the receiver.

require "lib" / "utilities" / "string"

The idea is not original, but we can’t remember where we first saw it.

Waves::Utilities::Symbol defines the same method, allowing for :files / ‘afilename.txt’



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

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

#camel_caseObject

produces StringsLikeThis



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

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

#lower_camel_caseObject

produces stringsLikeThis



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

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

#pluralObject Also known as: pluralize



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

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

#singularObject Also known as: singularize



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

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

#snake_caseObject

produces strings_like_this



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

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

#textObject



51
52
53
# File 'lib/utilities/string.rb', line 51

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

#title_caseObject



47
48
49
# File 'lib/utilities/string.rb', line 47

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