Class: String
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
-
#/(string) ⇒ Object
Does a File.join on the two arguments joined by the /.
- #camel_case ⇒ Object
- #lower_camel_case ⇒ Object
- #plural ⇒ Object (also: #pluralize)
- #singular ⇒ Object (also: #singularize)
- #snake_case ⇒ Object
- #text ⇒ Object
- #title_case ⇒ Object
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_case ⇒ Object
31 32 33 |
# File 'lib/utilities/string.rb', line 31 def camel_case lower_camel_case.gsub(/^([a-z])/) { $1.upcase } end |
#lower_camel_case ⇒ Object
27 28 29 |
# File 'lib/utilities/string.rb', line 27 def lower_camel_case gsub(/(_)(\w)/) { $2.upcase } end |
#plural ⇒ Object Also known as: pluralize
21 22 23 |
# File 'lib/utilities/string.rb', line 21 def plural Inflect::English.plural(self) end |
#singular ⇒ Object Also known as: singularize
15 16 17 |
# File 'lib/utilities/string.rb', line 15 def singular Inflect::English.singular(self) end |
#snake_case ⇒ Object
35 36 37 |
# File 'lib/utilities/string.rb', line 35 def snake_case gsub(/([a-z\d])([A-Z])/){ "#{$1}_#{$2}"}.tr("-", "_").downcase end |
#text ⇒ Object
43 44 45 |
# File 'lib/utilities/string.rb', line 43 def text gsub(/[\_\-\.\:]/,' ') end |
#title_case ⇒ Object
39 40 41 |
# File 'lib/utilities/string.rb', line 39 def title_case gsub(/(^|\s)\s*([a-z])/) { $1 + $2.upcase } end |