doc/api/classes/ActiveSupport/CoreExtensions/String/Inflections.html
<?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>
"ScaleScore".tableize => "scale_scores"
</pre>
</div>
</div>
<div id="method-list">
<h3 class="section-bar">Methods</h3>
<div class="name-list">
<a href="#M000430">camelcase</a>
<a href="#M000429">camelize</a>
<a href="#M000437">classify</a>
<a href="#M000440">constantize</a>
<a href="#M000434">dasherize</a>
<a href="#M000435">demodulize</a>
<a href="#M000439">foreign_key</a>
<a href="#M000438">humanize</a>
<a href="#M000427">pluralize</a>
<a href="#M000428">singularize</a>
<a href="#M000436">tableize</a>
<a href="#M000432">titlecase</a>
<a href="#M000431">titleize</a>
<a href="#M000433">underscore</a>
</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 ":lower"
then <a href="Inflections.html#M000429">camelize</a> produces
lowerCamelCase.
</p>
<p>
<a href="Inflections.html#M000429">camelize</a> will also convert
’/’ to ’::’ which is useful for converting paths to
namespaces
</p>
<p>
Examples
</p>
<pre>
"active_record".camelize #=> "ActiveRecord"
"active_record".camelize(:lower) #=> "activeRecord"
"active_record/errors".camelize #=> "ActiveRecord::Errors"
"active_record/errors".camelize(:lower) #=> "activeRecord::Errors"
</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>
"egg_and_hams".classify #=> "EggAndHam"
"post".classify #=> "Post"
</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>
"Module".constantize #=> Module
"Class".constantize #=> 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>
"puni_puni" #=> "puni-puni"
</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>
"ActiveRecord::CoreExtensions::String::Inflections".demodulize #=> "Inflections"
"Inflections".demodulize #=> "Inflections"
</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 ‘_’ between the name and ‘id’.
</p>
<p>
Examples
</p>
<pre>
"Message".foreign_key #=> "message_id"
"Message".foreign_key(false) #=> "messageid"
"Admin::Post".foreign_key #=> "post_id"
</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>
"employee_salary" #=> "Employee salary"
"author_id" #=> "Author"
</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>
"post".pluralize #=> "posts"
"octopus".pluralize #=> "octopi"
"sheep".pluralize #=> "sheep"
"words".pluralize #=> "words"
"the blue mailman".pluralize #=> "the blue mailmen"
"CamelOctopus".pluralize #=> "CamelOctopi"
</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>
"posts".singularize #=> "post"
"octopi".singularize #=> "octopus"
"sheep".singluarize #=> "sheep"
"word".singluarize #=> "word"
"the blue mailmen".singularize #=> "the blue mailman"
"CamelOctopi".singularize #=> "CamelOctopus"
</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>
"RawScaledScorer".tableize #=> "raw_scaled_scorers"
"egg_and_ham".tableize #=> "egg_and_hams"
"fancyCategory".tableize #=> "fancy_categories"
</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>
"man from the boondocks".titleize #=> "Man From The Boondocks"
"x-men: the last stand".titleize #=> "X Men: The Last Stand"
</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 ’::’ to ’/’ to convert namespaces to paths.
</p>
<p>
Examples
</p>
<pre>
"ActiveRecord".underscore #=> "active_record"
"ActiveRecord::Errors".underscore #=> 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>