diff options
author | Xavier Noria <fxn@hashref.com> | 2010-07-22 01:27:02 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2010-07-22 01:28:31 +0200 |
commit | b456877cfb7e0cb0bab9ffd5674abd23caba0ab4 (patch) | |
tree | 329cbe815d549275cc0dc6aa0f75c18d8954f105 /activesupport/lib/active_support | |
parent | b72cc472f762a6201e744b2def7467afb363b625 (diff) | |
download | rails-b456877cfb7e0cb0bab9ffd5674abd23caba0ab4.tar.gz rails-b456877cfb7e0cb0bab9ffd5674abd23caba0ab4.tar.bz2 rails-b456877cfb7e0cb0bab9ffd5674abd23caba0ab4.zip |
camelize and underscore are sort of inverse of each other, but not in a mathematical sense [#5174 state:resolved]
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/inflector/methods.rb | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/inflector/methods.rb b/activesupport/lib/active_support/inflector/methods.rb index b3dc5b2f3a..de49750083 100644 --- a/activesupport/lib/active_support/inflector/methods.rb +++ b/activesupport/lib/active_support/inflector/methods.rb @@ -20,6 +20,11 @@ module ActiveSupport # "active_record".camelize(:lower) # => "activeRecord" # "active_record/errors".camelize # => "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) if first_letter_in_uppercase lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } @@ -28,13 +33,18 @@ module ActiveSupport 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. # # Examples: # "ActiveRecord".underscore # => "active_record" # "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) word = camel_cased_word.to_s.dup word.gsub!(/::/, '/') |