Module: DataMetaXtra::Str
- Defined in:
- lib/dataMetaXtra.rb
Overview
Adding some methods to the standard Ruby String class, useful to generate code.
For more useful String related methods, see ActiveSupport’s Inflections
Class Method Summary collapse
-
.camelize(original) ⇒ Object
Turns underscored into camelcase, with first letter of the string and each after underscore turned uppercase and the rest lowercase.
-
.capFirst(original) ⇒ Object
Capitalize just first letter, leave everything else as it is.
-
.downCaseFirst(original) ⇒ Object
turn the first letter lowercase, leave everything else as it is.
-
.variablize(original) ⇒ Object
Same as camelize but makes sure that the first letter stays lowercase, useful for making variable names.
Class Method Details
.camelize(original) ⇒ Object
Turns underscored into camelcase, with first letter of the string and each after underscore turned uppercase and the rest lowercase. Useful for making class names.
Note that there is one good implementation in the ActiveSupport gem too.
Examples:
-
this_one_var=>ThisOneVar -
That_oThEr_vAR=>ThatOtherVar
See also variablize.
140 141 142 143 144 |
# File 'lib/dataMetaXtra.rb', line 140 def camelize(original) return original.downcase.capitalize if original =~ /[A-Z]+/ && original !~ /_/ return original.capitalize if original !~ /_/ original.split('_').map { |e| e.capitalize }.join end |
.capFirst(original) ⇒ Object
Capitalize just first letter, leave everything else as it is. In contrast to the standard method capitalize which leaves the tail lowercased.
118 119 120 |
# File 'lib/dataMetaXtra.rb', line 118 def capFirst(original) original[0].chr.upcase + original[1..-1] end |
.downCaseFirst(original) ⇒ Object
turn the first letter lowercase, leave everything else as it is.
123 124 125 |
# File 'lib/dataMetaXtra.rb', line 123 def downCaseFirst(original) original[0].chr.downcase + original[1..-1] end |
.variablize(original) ⇒ Object
Same as camelize but makes sure that the first letter stays lowercase, useful for making variable names.
Example:
-
That_oTHer_vAr=>thatOtherVar
See also camelize.
155 156 157 158 159 160 |
# File 'lib/dataMetaXtra.rb', line 155 def variablize(original) return original.downcase if original =~ /[A-Z]+/ && original !~ /_/ return original[0].downcase + original[1..-1] if original !~ /_/ camelized = original.split('_').map { |e| e.capitalize }.join camelized[0].downcase + camelized[1..-1] end |