doc/api/classes/ActiveSupport/CoreExtensions/String/Inflections.html

Summary

Maintainability
Test Coverage
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: ActiveSupport::CoreExtensions::String::Inflections</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href="../../../.././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;
    
    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }
  
  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
  
  // ]]>
  </script>

</head>
<body>



    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">ActiveSupport::CoreExtensions::String::Inflections</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>
                <a href="../../../../files/vendor/rails/activesupport/lib/active_support/core_ext/string/inflections_rb.html">
                vendor/rails/activesupport/lib/active_support/core_ext/string/inflections.rb
                </a>
        <br />
            </td>
        </tr>

        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">



  <div id="contextContent">

    <div id="description">
      <p>
String inflections define new methods on the String class to transform
names for different purposes. For instance, you can figure out the name of
a database from the name of a class.
</p>
<pre>
  &quot;ScaleScore&quot;.tableize =&gt; &quot;scale_scores&quot;
</pre>

    </div>


   </div>

    <div id="method-list">
      <h3 class="section-bar">Methods</h3>

      <div class="name-list">
      <a href="#M000430">camelcase</a>&nbsp;&nbsp;
      <a href="#M000429">camelize</a>&nbsp;&nbsp;
      <a href="#M000437">classify</a>&nbsp;&nbsp;
      <a href="#M000440">constantize</a>&nbsp;&nbsp;
      <a href="#M000434">dasherize</a>&nbsp;&nbsp;
      <a href="#M000435">demodulize</a>&nbsp;&nbsp;
      <a href="#M000439">foreign_key</a>&nbsp;&nbsp;
      <a href="#M000438">humanize</a>&nbsp;&nbsp;
      <a href="#M000427">pluralize</a>&nbsp;&nbsp;
      <a href="#M000428">singularize</a>&nbsp;&nbsp;
      <a href="#M000436">tableize</a>&nbsp;&nbsp;
      <a href="#M000432">titlecase</a>&nbsp;&nbsp;
      <a href="#M000431">titleize</a>&nbsp;&nbsp;
      <a href="#M000433">underscore</a>&nbsp;&nbsp;
      </div>
    </div>

  </div>


    <!-- if includes -->

    <div id="section">





      


    <!-- if method_list -->
    <div id="methods">
      <h3 class="section-bar">Public Instance methods</h3>

      <div id="method-M000430" class="method-detail">
        <a name="M000430"></a>

        <div class="method-heading">
          <span class="method-name">camelcase</span><span class="method-args">(first_letter = :upper)</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="Inflections.html#M000429">camelize</a>
</p>
        </div>
      </div>

      <div id="method-M000429" class="method-detail">
        <a name="M000429"></a>

        <div class="method-heading">
          <a href="#M000429" class="method-signature">
          <span class="method-name">camelize</span><span class="method-args">(first_letter = :upper)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
By default, <a href="Inflections.html#M000429">camelize</a> converts
strings to UpperCamelCase. If the argument to <a
href="Inflections.html#M000429">camelize</a> is set to &quot;:lower&quot;
then <a href="Inflections.html#M000429">camelize</a> produces
lowerCamelCase.
</p>
<p>
<a href="Inflections.html#M000429">camelize</a> will also convert
&#8217;/&#8217; to &#8217;::&#8217; which is useful for converting paths to
namespaces
</p>
<p>
Examples
</p>
<pre>
  &quot;active_record&quot;.camelize #=&gt; &quot;ActiveRecord&quot;
  &quot;active_record&quot;.camelize(:lower) #=&gt; &quot;activeRecord&quot;
  &quot;active_record/errors&quot;.camelize #=&gt; &quot;ActiveRecord::Errors&quot;
  &quot;active_record/errors&quot;.camelize(:lower) #=&gt; &quot;activeRecord::Errors&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000429-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000429-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/string/inflections.rb, line 46</span>
46:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">camelize</span>(<span class="ruby-identifier">first_letter</span> = <span class="ruby-identifier">:upper</span>)
47:           <span class="ruby-keyword kw">case</span> <span class="ruby-identifier">first_letter</span>
48:             <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:upper</span> <span class="ruby-keyword kw">then</span> <span class="ruby-constant">Inflector</span>.<span class="ruby-identifier">camelize</span>(<span class="ruby-keyword kw">self</span>, <span class="ruby-keyword kw">true</span>)
49:             <span class="ruby-keyword kw">when</span> <span class="ruby-identifier">:lower</span> <span class="ruby-keyword kw">then</span> <span class="ruby-constant">Inflector</span>.<span class="ruby-identifier">camelize</span>(<span class="ruby-keyword kw">self</span>, <span class="ruby-keyword kw">false</span>)
50:           <span class="ruby-keyword kw">end</span>
51:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000437" class="method-detail">
        <a name="M000437"></a>

        <div class="method-heading">
          <a href="#M000437" class="method-signature">
          <span class="method-name">classify</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Create a class name from a table name like Rails does for table names to
models. Note that this returns a string and not a Class. (To convert to an
actual class follow <a href="Inflections.html#M000437">classify</a> with <a
href="Inflections.html#M000440">constantize</a>.)
</p>
<p>
Examples
</p>
<pre>
  &quot;egg_and_hams&quot;.classify #=&gt; &quot;EggAndHam&quot;
  &quot;post&quot;.classify #=&gt; &quot;Post&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000437-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000437-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/string/inflections.rb, line 114</span>
114:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">classify</span>
115:           <span class="ruby-constant">Inflector</span>.<span class="ruby-identifier">classify</span>(<span class="ruby-keyword kw">self</span>)
116:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000440" class="method-detail">
        <a name="M000440"></a>

        <div class="method-heading">
          <a href="#M000440" class="method-signature">
          <span class="method-name">constantize</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
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.
</p>
<p>
Examples
</p>
<pre>
  &quot;Module&quot;.constantize #=&gt; Module
  &quot;Class&quot;.constantize #=&gt; Class
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000440-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000440-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/string/inflections.rb, line 147</span>
147:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">constantize</span>
148:           <span class="ruby-constant">Inflector</span>.<span class="ruby-identifier">constantize</span>(<span class="ruby-keyword kw">self</span>)
149:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000434" class="method-detail">
        <a name="M000434"></a>

        <div class="method-heading">
          <a href="#M000434" class="method-signature">
          <span class="method-name">dasherize</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Replaces underscores with dashes in the string.
</p>
<p>
Example
</p>
<pre>
  &quot;puni_puni&quot; #=&gt; &quot;puni-puni&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000434-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000434-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/string/inflections.rb, line 83</span>
83:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">dasherize</span>
84:           <span class="ruby-constant">Inflector</span>.<span class="ruby-identifier">dasherize</span>(<span class="ruby-keyword kw">self</span>)
85:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000435" class="method-detail">
        <a name="M000435"></a>

        <div class="method-heading">
          <a href="#M000435" class="method-signature">
          <span class="method-name">demodulize</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Removes the module part from the expression in the string
</p>
<p>
Examples
</p>
<pre>
  &quot;ActiveRecord::CoreExtensions::String::Inflections&quot;.demodulize #=&gt; &quot;Inflections&quot;
  &quot;Inflections&quot;.demodulize #=&gt; &quot;Inflections&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000435-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000435-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/string/inflections.rb, line 92</span>
92:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">demodulize</span>
93:           <span class="ruby-constant">Inflector</span>.<span class="ruby-identifier">demodulize</span>(<span class="ruby-keyword kw">self</span>)
94:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000439" class="method-detail">
        <a name="M000439"></a>

        <div class="method-heading">
          <a href="#M000439" class="method-signature">
          <span class="method-name">foreign_key</span><span class="method-args">(separate_class_name_and_id_with_underscore = true)</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Creates a foreign key name from a class name.
<tt>separate_class_name_and_id_with_underscore</tt> sets whether the method
should put &#8216;_&#8217; between the name and &#8216;id&#8217;.
</p>
<p>
Examples
</p>
<pre>
  &quot;Message&quot;.foreign_key #=&gt; &quot;message_id&quot;
  &quot;Message&quot;.foreign_key(false) #=&gt; &quot;messageid&quot;
  &quot;Admin::Post&quot;.foreign_key #=&gt; &quot;post_id&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000439-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000439-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/string/inflections.rb, line 136</span>
136:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">foreign_key</span>(<span class="ruby-identifier">separate_class_name_and_id_with_underscore</span> = <span class="ruby-keyword kw">true</span>)
137:           <span class="ruby-constant">Inflector</span>.<span class="ruby-identifier">foreign_key</span>(<span class="ruby-keyword kw">self</span>, <span class="ruby-identifier">separate_class_name_and_id_with_underscore</span>)
138:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000438" class="method-detail">
        <a name="M000438"></a>

        <div class="method-heading">
          <a href="#M000438" class="method-signature">
          <span class="method-name">humanize</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Capitalizes the first word and turns underscores into spaces and strips
_id. Like <a href="Inflections.html#M000431">titleize</a>, this is meant
for creating pretty output.
</p>
<p>
Examples
</p>
<pre>
  &quot;employee_salary&quot; #=&gt; &quot;Employee salary&quot;
  &quot;author_id&quot; #=&gt; &quot;Author&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000438-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000438-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/string/inflections.rb, line 124</span>
124:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">humanize</span>
125:           <span class="ruby-constant">Inflector</span>.<span class="ruby-identifier">humanize</span>(<span class="ruby-keyword kw">self</span>)
126:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000427" class="method-detail">
        <a name="M000427"></a>

        <div class="method-heading">
          <a href="#M000427" class="method-signature">
          <span class="method-name">pluralize</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Returns the plural form of the word in the string.
</p>
<p>
Examples
</p>
<pre>
  &quot;post&quot;.pluralize #=&gt; &quot;posts&quot;
  &quot;octopus&quot;.pluralize #=&gt; &quot;octopi&quot;
  &quot;sheep&quot;.pluralize #=&gt; &quot;sheep&quot;
  &quot;words&quot;.pluralize #=&gt; &quot;words&quot;
  &quot;the blue mailman&quot;.pluralize #=&gt; &quot;the blue mailmen&quot;
  &quot;CamelOctopus&quot;.pluralize #=&gt; &quot;CamelOctopi&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000427-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000427-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/string/inflections.rb, line 19</span>
19:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">pluralize</span>
20:           <span class="ruby-constant">Inflector</span>.<span class="ruby-identifier">pluralize</span>(<span class="ruby-keyword kw">self</span>)
21:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000428" class="method-detail">
        <a name="M000428"></a>

        <div class="method-heading">
          <a href="#M000428" class="method-signature">
          <span class="method-name">singularize</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
The reverse of <a href="Inflections.html#M000427">pluralize</a>, returns
the singular form of a word in a string.
</p>
<p>
Examples
</p>
<pre>
  &quot;posts&quot;.singularize #=&gt; &quot;post&quot;
  &quot;octopi&quot;.singularize #=&gt; &quot;octopus&quot;
  &quot;sheep&quot;.singluarize #=&gt; &quot;sheep&quot;
  &quot;word&quot;.singluarize #=&gt; &quot;word&quot;
  &quot;the blue mailmen&quot;.singularize #=&gt; &quot;the blue mailman&quot;
  &quot;CamelOctopi&quot;.singularize #=&gt; &quot;CamelOctopus&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000428-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000428-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/string/inflections.rb, line 32</span>
32:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">singularize</span>
33:           <span class="ruby-constant">Inflector</span>.<span class="ruby-identifier">singularize</span>(<span class="ruby-keyword kw">self</span>)
34:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000436" class="method-detail">
        <a name="M000436"></a>

        <div class="method-heading">
          <a href="#M000436" class="method-signature">
          <span class="method-name">tableize</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Create the name of a table like Rails does for models to table names. This
method uses the <a href="Inflections.html#M000427">pluralize</a> method on
the last word in the string.
</p>
<p>
Examples
</p>
<pre>
  &quot;RawScaledScorer&quot;.tableize #=&gt; &quot;raw_scaled_scorers&quot;
  &quot;egg_and_ham&quot;.tableize #=&gt; &quot;egg_and_hams&quot;
  &quot;fancyCategory&quot;.tableize #=&gt; &quot;fancy_categories&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000436-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000436-source">
<pre>
     <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/string/inflections.rb, line 103</span>
103:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">tableize</span>
104:           <span class="ruby-constant">Inflector</span>.<span class="ruby-identifier">tableize</span>(<span class="ruby-keyword kw">self</span>)
105:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000432" class="method-detail">
        <a name="M000432"></a>

        <div class="method-heading">
          <span class="method-name">titlecase</span><span class="method-args">()</span>
        </div>
      
        <div class="method-description">
          <p>
Alias for <a href="Inflections.html#M000431">titleize</a>
</p>
        </div>
      </div>

      <div id="method-M000431" class="method-detail">
        <a name="M000431"></a>

        <div class="method-heading">
          <a href="#M000431" class="method-signature">
          <span class="method-name">titleize</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
Capitalizes all the words and replaces some characters in the string to
create a nicer looking title. Titleize is meant for creating pretty output.
It is not used in the Rails internals.
</p>
<p>
<a href="Inflections.html#M000431">titleize</a> is also aliased as as <a
href="Inflections.html#M000432">titlecase</a>
</p>
<p>
Examples
</p>
<pre>
  &quot;man from the boondocks&quot;.titleize #=&gt; &quot;Man From The Boondocks&quot;
  &quot;x-men: the last stand&quot;.titleize #=&gt; &quot;X Men: The Last Stand&quot;
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000431-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000431-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/string/inflections.rb, line 63</span>
63:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">titleize</span>
64:           <span class="ruby-constant">Inflector</span>.<span class="ruby-identifier">titleize</span>(<span class="ruby-keyword kw">self</span>)
65:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>

      <div id="method-M000433" class="method-detail">
        <a name="M000433"></a>

        <div class="method-heading">
          <a href="#M000433" class="method-signature">
          <span class="method-name">underscore</span><span class="method-args">()</span>
          </a>
        </div>
      
        <div class="method-description">
          <p>
The reverse of <tt><a href="Inflections.html#M000429">camelize</a></tt>.
Makes an underscored form from the expression in the string.
</p>
<p>
Changes &#8217;::&#8217; to &#8217;/&#8217; to convert namespaces to paths.
</p>
<p>
Examples
</p>
<pre>
  &quot;ActiveRecord&quot;.underscore #=&gt; &quot;active_record&quot;
  &quot;ActiveRecord::Errors&quot;.underscore #=&gt; active_record/errors
</pre>
          <p><a class="source-toggle" href="#"
            onclick="toggleCode('M000433-source');return false;">[Source]</a></p>
          <div class="method-source-code" id="M000433-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/rails/activesupport/lib/active_support/core_ext/string/inflections.rb, line 75</span>
75:         <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">underscore</span>
76:           <span class="ruby-constant">Inflector</span>.<span class="ruby-identifier">underscore</span>(<span class="ruby-keyword kw">self</span>)
77:         <span class="ruby-keyword kw">end</span>
</pre>
          </div>
        </div>
      </div>


    </div>


  </div>


<div id="validator-badges">
  <p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
</div>

</body>
</html>