aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2010-07-22 01:27:02 +0200
committerXavier Noria <fxn@hashref.com>2010-07-22 01:27:02 +0200
commit56669ec3048de316918ec5ad554fff83d757911b (patch)
tree3a7608b73292ac61c1ac8a414799cba16f045ebb
parent5cba6635787ed65a420d48ea0e93ff4af2ba59f2 (diff)
downloadrails-56669ec3048de316918ec5ad554fff83d757911b.tar.gz
rails-56669ec3048de316918ec5ad554fff83d757911b.tar.bz2
rails-56669ec3048de316918ec5ad554fff83d757911b.zip
camelize and underscore are sort of inverse of each other, but not in a mathematical sense [#5174 state:resolved]
-rw-r--r--activesupport/lib/active_support/inflector/methods.rb12
-rw-r--r--railties/guides/source/active_support_core_extensions.textile6
2 files changed, 16 insertions, 2 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!(/::/, '/')
diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile
index a0ed8d6a90..297dad2ccc 100644
--- a/railties/guides/source/active_support_core_extensions.textile
+++ b/railties/guides/source/active_support_core_extensions.textile
@@ -1469,13 +1469,15 @@ end
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+.
NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
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>
"Product".underscore # => "product"
@@ -1508,6 +1510,8 @@ def load_missing_constant(from_mod, const_name)
end
</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+.
h5. +titleize+