Module: Waves::Utilities::String
- Included in:
- String
- Defined in:
- lib/utilities/string.rb
Overview
Utility methods mixed into String.
Instance Method Summary collapse
-
#/(string) ⇒ Object
Syntactic sugar for using File.join to concatenate the argument to the receiver.
-
#camel_case ⇒ Object
produces StringsLikeThis.
-
#lower_camel_case ⇒ Object
produces stringsLikeThis.
- #plural ⇒ Object (also: #pluralize)
- #singular ⇒ Object (also: #singularize)
-
#snake_case ⇒ Object
produces strings_like_this.
- #text ⇒ Object
- #title_case ⇒ Object
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_case ⇒ Object
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_case ⇒ Object
produces stringsLikeThis
33 34 35 |
# File 'lib/utilities/string.rb', line 33 def lower_camel_case gsub(/(_)(\w)/) { $2.upcase } end |
#plural ⇒ Object Also known as: pluralize
26 27 28 |
# File 'lib/utilities/string.rb', line 26 def plural Waves::Inflect::English.plural(self) end |
#singular ⇒ Object Also known as: singularize
20 21 22 |
# File 'lib/utilities/string.rb', line 20 def singular Waves::Inflect::English.singular(self) end |
#snake_case ⇒ Object
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 |
#text ⇒ Object
51 52 53 |
# File 'lib/utilities/string.rb', line 51 def text gsub(/[\_\-\.\:]/,' ') end |
#title_case ⇒ Object
47 48 49 |
# File 'lib/utilities/string.rb', line 47 def title_case gsub(/(^|\s)\s*([a-z])/) { $1 + $2.upcase } end |