module DataMetaXtra::Str

Adding some methods to the standard Ruby String class, useful to generate code.

For more useful String related methods, see ActiveSupport’s Inflections

Public Class Methods

camelize(original) click to toggle source

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.

# 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) click to toggle source

Capitalize just first letter, leave everything else as it is. In contrast to the standard method capitalize which leaves the tail lowercased.

# File lib/dataMetaXtra.rb, line 118
def capFirst(original)
    original[0].chr.upcase + original[1..-1]
end
downCaseFirst(original) click to toggle source

turn the first letter lowercase, leave everything else as it is.

# File lib/dataMetaXtra.rb, line 123
def downCaseFirst(original)
    original[0].chr.downcase + original[1..-1]
end
variablize(original) click to toggle source

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.

# 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

Private Instance Methods

camelize(original) click to toggle source

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.

# 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) click to toggle source

Capitalize just first letter, leave everything else as it is. In contrast to the standard method capitalize which leaves the tail lowercased.

# File lib/dataMetaXtra.rb, line 118
def capFirst(original)
    original[0].chr.upcase + original[1..-1]
end
downCaseFirst(original) click to toggle source

turn the first letter lowercase, leave everything else as it is.

# File lib/dataMetaXtra.rb, line 123
def downCaseFirst(original)
    original[0].chr.downcase + original[1..-1]
end
variablize(original) click to toggle source

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.

# 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