Class: String
Overview
:nodoc:
Instance Method Summary collapse
- #blank? ⇒ Boolean
-
#camelize(lower_case_and_underscored_word = self, first_letter_in_uppercase = true) ⇒ Object
By default,
camelizeconverts strings to UpperCamelCase. -
#constantize ⇒ Object
Constantize tries to find a declared constant with the name specified in the string.
-
#underscore ⇒ Object
The reverse of
camelize.
Instance Method Details
#blank? ⇒ Boolean
139 140 141 |
# File 'lib/awsbase/support.rb', line 139 def blank? empty? || strip.empty? end |
#camelize(lower_case_and_underscored_word = self, first_letter_in_uppercase = true) ⇒ Object
By default, camelize converts strings to UpperCamelCase. If the argument to camelize is set to :lower then camelize produces lowerCamelCase.
camelize will also convert ‘/’ to ‘::’ which is useful for converting paths to namespaces.
Examples:
"active_record".camelize # => "ActiveRecord"
"active_record".camelize(:lower) # => "activeRecord"
"active_record/errors".camelize # => "ActiveRecord::Errors"
"active_record/errors".camelize(:lower) # => "activeRecord::Errors"
64 65 66 67 68 69 70 |
# File 'lib/awsbase/support.rb', line 64 def camelize(lower_case_and_underscored_word=self, first_letter_in_uppercase = true) if first_letter_in_uppercase lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } else lower_case_and_underscored_word.first.downcase + camelize(lower_case_and_underscored_word)[1..-1] end end |
#constantize ⇒ Object
Constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.
Examples
"Module".constantize #=> Module
"Class".constantize #=> Class
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/awsbase/support.rb', line 42 def constantize() camel_cased_word = self names = camel_cased_word.split('::') names.shift if names.empty? || names.first.empty? constant = Object names.each do |name| constant = constant.const_get(name, false) || constant.const_missing(name) end constant end |
#underscore ⇒ Object
The reverse of camelize. Makes an underscored, lowercase form from the expression in the string.
Changes ‘::’ to ‘/’ to convert namespaces to paths.
Examples:
"ActiveRecord".underscore # => "active_record"
"ActiveRecord::Errors".underscore # => active_record/errors
79 80 81 82 83 84 85 |
# File 'lib/awsbase/support.rb', line 79 def underscore self.to_s.gsub(/::/, '/'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_"). downcase end |