Permalink
Browse files

AS guide: documents String#demodulize

  • Loading branch information...
1 parent 44bd3bd commit aab42ccdab084dbd661d3e21d9fce15d0f3f6a06 @fxn fxn committed Mar 1, 2010
Showing with 25 additions and 0 deletions.
  1. +25 −0 railties/guides/source/active_support_core_extensions.textile
@@ -1412,6 +1412,31 @@ end
NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+h5. +demodulize+
+
+Given a string with a qualified constant reference expression, +demodulize+ returns the very constant name, that is, the rightmost part of it:
+
+<ruby>
+"Product".demodulize # => "Product"
+"Backoffice::UsersController".demodulize # => "UsersController"
+"Admin::Hotel::ReservationUtils".demodulize # => "ReservationUtils"
+</ruby>
+
+Active Record for example uses this method to compute the name of a counter cache column:
+
+<ruby>
+# active_record/reflection.rb
+def counter_cache_column
+ if options[:counter_cache] == true
+ "#{active_record.name.demodulize.underscore.pluralize}_count"
+ elsif options[:counter_cache]
+ options[:counter_cache]
+ end
+end
+</ruby>
+
+NOTE: Defined in +active_support/core_ext/string/inflections.rb+.
+
h3. Extensions to +Numeric+
h4. Bytes

0 comments on commit aab42cc

Please sign in to comment.