<?xml version="1.0" encoding="iso-8859-1"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>dekstop weblog : How to Use the Rails Inflector in Your Ruby Scripts</title>
    <link>http://dekstop.de/weblog/2005/12/rails_inflector_in_ruby_scripts/</link>
    <description>I&apos;ve written a Ruby script that extracts keywords from a MovableType-exported plaintext database, and while doing so wanted to include the Rails Inflector so that the script could merge singular and plural versions of the same word. It wasn&apos;t that obvious to me how to include the Inflector in Ruby ...</description>
    <dc:language>en-us</dc:language>
    <dc:rights>Copyright 2005 Martin Dittus</dc:rights>
    <lastBuildDate>Tue, 06 Dec 2005 12:47:47 GMT</lastBuildDate>
    <generator>MicroLinks 5.6 (dekstop.de)</generator>
    <managingEditor>public&#64;dekstop&#46;de</managingEditor>
    <webMaster>public&#64;dekstop&#46;de</webMaster>



    <item>
      <title>How to Use the Rails Inflector in Your Ruby Scripts</title>
      <link>http://dekstop.de/weblog/2005/12/rails_inflector_in_ruby_scripts/</link> 
      <description><![CDATA[<p>I've written a Ruby script that extracts keywords from a MovableType-exported plaintext database, and while doing so wanted to include the <a href="http://api.rubyonrails.com/classes/Inflector.html">Rails Inflector</a> so that the script could merge singular and plural versions of the same word. It wasn't that obvious to me how to include the Inflector in Ruby scripts outside of Rails, and I searched for a while until I found the proper usage; so I'll document it here to save other Ruby newbies some time. </p>

<p>In the end it boiled down to finding the proper <tt>require</tt> statements -- I'm not sure if this is the best way though, so any comments are appreciated. Here's how you do it: </p>

<pre>
#!/usr/bin/ruby
require 'rubygems'
require 'active_support/inflector'

puts Inflector.singularize('inflections')
</pre>

<h3>Explanation</h3>

<p>I've found that you need to <tt>require 'rubygems'</tt> first, which is to be expected (as ActiveSupport, at least on my system, is a gem). But then it becomes unintuitive.</p>

<p>Because if you then attempt to <tt>require_gem 'activesupport'</tt>, the Inflector isn't made accessible: </p>

<pre>
$ irb
irb(main):001:0&gt; require 'rubygems'
=&gt; true
irb(main):002:0&gt; require_gem 'activesupport'
=&gt; true
irb(main):003:0&gt; Inflector
NameError: uninitialized constant Inflector
        from (irb):3
</pre>

<p>...and you can't <tt>require_gem 'active_support/inflector'</tt>, because that's not a valid gem:</p>

<pre>
irb(main):004:0&gt; require_gem 'active_support/inflector'
Gem::LoadError: Could not find RubyGem active_support/inflector (&gt; 0.0.0)
        from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems.rb:204:in `report_activate_error'
        from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems.rb:141:in `activate'
        from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems.rb:37:in `require_gem_with_options'
        from /opt/local/lib/ruby/vendor_ruby/1.8/rubygems.rb:31:in `require_gem'
        from (irb):4
</pre>

<p>...but you can do this instead (and it only works after loading the <tt>activesupport</tt> gem):</p>

<pre>
irb(main):005:0&gt; require 'active_support/inflector'
=&gt; true 
irb(main):006:0&gt; Inflector
=&gt; Inflector
irb(main):007:0&gt; Inflector.singularize('inflections')
=&gt; "inflection"
</pre>

<p>Even more confusing is the mixing of <tt>activesupport</tt> and <tt>active_support</tt> path names, but I assume that's the difference between the gem's name ("activesupport") versus its location on disk (<tt>.../active_support/</tt>).</p>

<p>I really should RTFM.</p>

<p><small><strong>Update</strong> 2006-06-29: I <a href="http://mikeburnscoder.wordpress.com/2006/06/09/response_from-the-opposite-of-respond_to-render-xml/">helped</a>!</small></p>]]></description>
      <dc:creator>Martin Dittus</dc:creator>
      <category>code</category>
      <category>stuff</category>
      <category>tools</category>
      
      <guid isPermaLink="true">http://dekstop.de/weblog/2005/12/rails_inflector_in_ruby_scripts/</guid>
      <pubDate>Tue, 06 Dec 2005 12:47:47 GMT</pubDate>
    </item>
  </channel>
</rss>
