Skip to content

Commit

Permalink
camelize and underscore are sort of inverse of each other, but not in…
Browse files Browse the repository at this point in the history
… a mathematical sense [#5174 state:resolved]
  • Loading branch information
fxn committed Jul 21, 2010
1 parent b72cc47 commit b456877
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
12 changes: 11 additions & 1 deletion activesupport/lib/active_support/inflector/methods.rb
Expand Up @@ -20,6 +20,11 @@ module Inflector
# "active_record".camelize(:lower) # => "activeRecord" # "active_record".camelize(:lower) # => "activeRecord"
# "active_record/errors".camelize # => "ActiveRecord::Errors" # "active_record/errors".camelize # => "ActiveRecord::Errors"
# "active_record/errors".camelize(:lower) # => "activeRecord::Errors" # "active_record/errors".camelize(:lower) # => "activeRecord::Errors"
#
# As a rule of thumb you can think of +camelize+ as the inverse of +underscore+,
# though there are cases where that does not hold:
#
# "SSLError".underscore.camelize # => "SslError"
def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
if first_letter_in_uppercase if first_letter_in_uppercase
lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
Expand All @@ -28,13 +33,18 @@ def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true)
end end
end end


# The reverse of +camelize+. Makes an underscored, lowercase form from the expression in the string. # Makes an underscored, lowercase form from the expression in the string.
# #
# Changes '::' to '/' to convert namespaces to paths. # Changes '::' to '/' to convert namespaces to paths.
# #
# Examples: # Examples:
# "ActiveRecord".underscore # => "active_record" # "ActiveRecord".underscore # => "active_record"
# "ActiveRecord::Errors".underscore # => active_record/errors # "ActiveRecord::Errors".underscore # => active_record/errors
#
# As a rule of thumb you can think of +underscore+ as the inverse of +camelize+,
# though there are cases where that does not hold:
#
# "SSLError".underscore.camelize # => "SslError"
def underscore(camel_cased_word) def underscore(camel_cased_word)
word = camel_cased_word.to_s.dup word = camel_cased_word.to_s.dup
word.gsub!(/::/, '/') word.gsub!(/::/, '/')
Expand Down
Expand Up @@ -1491,13 +1491,15 @@ end


That may be handy to compute method names in a language that follows that convention, for example JavaScript. That may be handy to compute method names in a language that follows that convention, for example JavaScript.


INFO: As a rule of thumb you can think of +camelize+ as the inverse of +underscore+, though there are cases where that does not hold: <tt>"SSLError".underscore.camelize</tt> gives back <tt>"SslError"</tt>.

+camelize+ is aliased to +camelcase+. +camelize+ is aliased to +camelcase+.


NOTE: Defined in +active_support/core_ext/string/inflections.rb+. NOTE: Defined in +active_support/core_ext/string/inflections.rb+.


h5. +underscore+ h5. +underscore+


The method +underscore+ is the inverse of +camelize+, explained above: The method +underscore+ goes the other way around, from camel case to paths:


<ruby> <ruby>
"Product".underscore # => "product" "Product".underscore # => "product"
Expand Down Expand Up @@ -1530,6 +1532,8 @@ def load_missing_constant(from_mod, const_name)
end end
</ruby> </ruby>


INFO: As a rule of thumb you can think of +underscore+ as the inverse of +camelize+, though there are cases where that does not hold. For example, <tt>"SSLError".underscore.camelize</tt> gives back <tt>"SslError"</tt>.

NOTE: Defined in +active_support/core_ext/string/inflections.rb+. NOTE: Defined in +active_support/core_ext/string/inflections.rb+.


h5. +titleize+ h5. +titleize+
Expand Down

0 comments on commit b456877

Please sign in to comment.