<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Curtis Summers &#187; rubyonrails</title>
	<atom:link href="http://csummers.com/category/rubyonrails/feed/" rel="self" type="application/rss+xml" />
	<link>http://csummers.com</link>
	<description>%w{life code ruby}.map { &#124;i&#124; "#{i} is awesome" }</description>
	<lastBuildDate>Tue, 16 Mar 2010 11:33:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Ruby: Recursive send</title>
		<link>http://csummers.com/2007/01/18/ruby-recursive-send/</link>
		<comments>http://csummers.com/2007/01/18/ruby-recursive-send/#comments</comments>
		<pubDate>Thu, 18 Jan 2007 20:02:25 +0000</pubDate>
		<dc:creator>Curt</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rubyonrails]]></category>

		<guid isPermaLink="false">http://www.csummers.org/index.php/2007/01/18/ruby-recursive-send/</guid>
		<description><![CDATA[Ruby&#8217;s Object#send allows dynamic calling of a method. This is very useful, but what if we wanted to call several levels deep on an object? For instance: # Normal call chain post.comments.first.commented_at &#160; # Dynamically with send? Have to call three times. post.send&#40;:comments&#41;.send&#40;:first&#41;.send&#40;:commented_at&#41; What if the number of calls to send is variable depending on [...]]]></description>
			<content:encoded><![CDATA[<p>Ruby&#8217;s Object#send allows dynamic calling of a method.  This is very useful, but what if we wanted to call several levels deep on an object?  For instance:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#008000; font-style:italic;"># Normal call chain</span>
post.<span style="color:#9900CC;">comments</span>.<span style="color:#9900CC;">first</span>.<span style="color:#9900CC;">commented_at</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Dynamically with send?  Have to call three times.</span>
post.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:comments</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:first</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:commented_at</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>What if the number of calls to send is variable depending on what we&#8217;re trying to show?  In one case we might need <span style="font-family: mono">post.posted_at</span> for the date, and in another case we might need <span style="font-family: mono">post.comments.first.commented_at</span> for the date.</p>
<p>How could we dynamically craft the definition of the methods to send if we don&#8217;t know how many calls to Object#send we&#8217;ll have?  We need a way to define an arbitrary number of method calls.</p>
<p>Behold, a recursive send:  Object#rsend</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> rsend<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>args, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
    obj = <span style="color:#0000FF; font-weight:bold;">self</span>
    args.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span>
      b = <span style="color:#006600; font-weight:bold;">&#40;</span>a.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">Array</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> a.<span style="color:#9900CC;">last</span>.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">Proc</span><span style="color:#006600; font-weight:bold;">&#41;</span> ? a.<span style="color:#9900CC;">pop</span> : block<span style="color:#006600; font-weight:bold;">&#41;</span>
      obj = obj.__send__<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>a, <span style="color:#006600; font-weight:bold;">&amp;</span>b<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    obj
  <span style="color:#9966CC; font-weight:bold;">end</span>
  alias_method <span style="color:#ff3333; font-weight:bold;">:__rsend__</span>, <span style="color:#ff3333; font-weight:bold;">:rsend</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Each argument passed to Object#rsend is an array with the symbols and arguments that will be passed on to Object#send:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">post.<span style="color:#9900CC;">rsend</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:comments</span><span style="color:#006600; font-weight:bold;">&#93;</span>,<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:first</span><span style="color:#006600; font-weight:bold;">&#93;</span>,<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:commented_at</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>If there are no arguments to be passed on to send, the array brackets can be omitted:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">post.<span style="color:#9900CC;">rsend</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:comments</span>, <span style="color:#ff3333; font-weight:bold;">:first</span>, <span style="color:#ff3333; font-weight:bold;">:commented_at</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Of course, in practice you&#8217;ll probably be defining your method call chain in one part of your code, putting it in a variable, and sending it to rsend with a splat*:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">the_date = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:comments</span>, <span style="color:#ff3333; font-weight:bold;">:first</span>, <span style="color:#ff3333; font-weight:bold;">:commented_at</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#...somewhere else in your code you've passed the_date along:</span>
post.<span style="color:#9900CC;">rsend</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">*</span>the_date<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>With arguments:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">a = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#006666;">0</span>,<span style="color:#006666;">1</span>,<span style="color:#006666;">2</span>,<span style="color:#006666;">3</span>,<span style="color:#006666;">4</span>,<span style="color:#006666;">5</span>,<span style="color:#006666;">6</span>,<span style="color:#006666;">7</span>,<span style="color:#006666;">8</span>,<span style="color:#006666;">9</span><span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
a.<span style="color:#9900CC;">rsend</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:slice</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">8</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; [2, 3, 4, 5, 6, 7, 8, 9]</span>
&nbsp;
a.<span style="color:#9900CC;">rsend</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:slice</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">8</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:slice</span>, <span style="color:#006666;">1</span>, <span style="color:#006666;">3</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;">#=&gt; [3, 4, 5]</span></pre></div></div>

<p>Object#send accepts a block.  What about blocks?  Pass in a proc:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">a.<span style="color:#9900CC;">rsend</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:map</span>, <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;">#=&gt; [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]</span>
&nbsp;
a.<span style="color:#9900CC;">rsend</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:map</span>, <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span>,
        <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:select</span>, <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x <span style="color:#006600; font-weight:bold;">%</span> <span style="color:#006666;">4</span> == <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;">#=&gt; [0, 4, 8, 12, 16]</span></pre></div></div>

<p>And, in an effort to make Object#rsend behave like Object#send for the simple case, you can send a regular block:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">a.<span style="color:#9900CC;">rsend</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:map</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#008000; font-style:italic;">#=&gt; [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]</span></pre></div></div>

<p>Caveat:  For the case needing parameters, Object#rsend does require an array, so:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">a.<span style="color:#9900CC;">rsend</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:slice</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">8</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># wrong, does not work like Object#send</span>
&nbsp;
a.<span style="color:#9900CC;">rsend</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:slice</span>, <span style="color:#006666;">2</span>, <span style="color:#006666;">8</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#008000; font-style:italic;"># right</span></pre></div></div>

<p>A quirk that I&#8217;ve left in for fun, but it might (and maybe should) change:  If providing a single block, that block will be called on every call unless you&#8217;ve already passed in a proc:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby">a.<span style="color:#9900CC;">rsend</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:map</span>, <span style="color:#ff3333; font-weight:bold;">:map</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#008000; font-style:italic;">#=&gt; [0, 4, 8, 12, 16, 20, 24, 28, 32, 36]</span>
&nbsp;
a.<span style="color:#9900CC;">rsend</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:map</span>, <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:map</span>, <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">proc</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x<span style="color:#006666;">+5</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#93;</span>, <span style="color:#ff3333; font-weight:bold;">:map</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span> x<span style="color:#006600; font-weight:bold;">*</span><span style="color:#006666;">2</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  <span style="color:#008000; font-style:italic;">#=&gt; [10, 14, 18, 22, 26, 30, 34, 38, 42, 46]</span>
  <span style="color:#008000; font-style:italic;">#outer block was called on first and third :map</span></pre></div></div>

<p>Can anyone come up with a good use for this call-the-block-each-time behavior?</p>
<p>Has anyone done this already?  I searched for such a thing and came up empty.  Maybe this method should be called something else?  I named it based on each call recursing down the chain of methods with a new object being returned for the next method to be sent to.</p>
<p>Suggestions and comments are welcome.</p>
]]></content:encoded>
			<wfw:commentRss>http://csummers.com/2007/01/18/ruby-recursive-send/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Extend String to use ActionView&#8217;s Text Helpers</title>
		<link>http://csummers.com/2006/08/07/extend-string-to-use-actionviews-text-helpers/</link>
		<comments>http://csummers.com/2006/08/07/extend-string-to-use-actionviews-text-helpers/#comments</comments>
		<pubDate>Mon, 07 Aug 2006 21:54:02 +0000</pubDate>
		<dc:creator>Curt</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[rubyonrails]]></category>

		<guid isPermaLink="false">http://www.csummers.org/index.php/2006/08/07/extend-string-to-use-actionviews-text-helpers/</guid>
		<description><![CDATA[Ruby on Rails and Ruby are an amazing combo. Here&#8217;s another example of why. ActionView&#8217;s TextHelper methods are useful, but I often need to use them in my controller or my model. For several of the TextHelper methods that expect a string as input, it makes sense to extend the String class. So, if I [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.rubyonrails.com/">Ruby on Rails</a> and <a href="http://www.ruby-lang.org/en/">Ruby</a> are an amazing combo.  Here&#8217;s another example of why.</p>
<p>ActionView&#8217;s TextHelper methods are useful, but I often need to use them in my controller or my model.  For several of the TextHelper methods that expect a string as input, it makes sense to extend the String class.</p>
<p>So, if I want to strip HTML tags, auto link any URLs, and then simple format a comment (in that order) before I save it in the database I can do:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#9966CC; font-weight:bold;">class</span> Comment <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
<span style="color:#008000; font-style:italic;">#...</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> before_save
    <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">text</span> = <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">text</span>.<span style="color:#9900CC;">strip_tags</span>.<span style="color:#9900CC;">auto_link</span>.<span style="color:#9900CC;">simple_format</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This method is much cleaner than including ActionView::Helpers::TextHelper in whatever class I&#8217;m in and passing the string as an argument to each method.</p>
<p>Below is the magic code.  Since TextHelper is a module, we create a Singleton class to reference the methods, create the wrapper methods in their own module, and finally include that module in the String class.  Note that not all TextHelper methods are included&#8211;just the ones that make sense.  Drop this code into a file and require it in your environment or within a plugin.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby"><span style="color:#008000; font-style:italic;"># ActionView Text Helpers are great!</span>
<span style="color:#008000; font-style:italic;"># Let's extend the String class to allow us to call</span>
<span style="color:#008000; font-style:italic;"># some of these methods directly on a String.</span>
<span style="color:#008000; font-style:italic;"># Note:</span>
<span style="color:#008000; font-style:italic;">#  - cycle-related methods are not included</span>
<span style="color:#008000; font-style:italic;">#  - concat is not included</span>
<span style="color:#008000; font-style:italic;">#  - pluralize is not included because it is in</span>
<span style="color:#008000; font-style:italic;">#       ActiveSupport String extensions already</span>
<span style="color:#008000; font-style:italic;">#       (though they differ).</span>
<span style="color:#008000; font-style:italic;">#  - markdown requires BlueCloth</span>
<span style="color:#008000; font-style:italic;">#  - textilize methods require RedCloth</span>
<span style="color:#008000; font-style:italic;"># Example:</span>
<span style="color:#008000; font-style:italic;"># &quot;&lt;b&gt;coolness&lt;/b&gt;&quot;.strip_tags -&gt; &quot;coolness&quot;</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'singleton'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Singleton to be called in wrapper module</span>
<span style="color:#9966CC; font-weight:bold;">class</span> TextHelperSingleton
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#CC00FF; font-weight:bold;">Singleton</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActionView::Helpers::TextHelper</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActionView::Helpers::TagHelper</span> <span style="color:#008000; font-style:italic;">#tag_options needed by auto_link</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Wrapper module</span>
<span style="color:#9966CC; font-weight:bold;">module</span> MyExtensions <span style="color:#008000; font-style:italic;">#:nodoc:</span>
  <span style="color:#9966CC; font-weight:bold;">module</span> CoreExtensions <span style="color:#008000; font-style:italic;">#:nodoc:</span>
    <span style="color:#9966CC; font-weight:bold;">module</span> <span style="color:#CC0066; font-weight:bold;">String</span> <span style="color:#008000; font-style:italic;">#:nodoc:</span>
      <span style="color:#9966CC; font-weight:bold;">module</span> TextHelper
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">def</span> auto_link<span style="color:#006600; font-weight:bold;">&#40;</span>link = <span style="color:#ff3333; font-weight:bold;">:all</span>, href_options = <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
          TextHelperSingleton.<span style="color:#9900CC;">instance</span>.<span style="color:#9900CC;">auto_link</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>, link, href_options, <span style="color:#006600; font-weight:bold;">&amp;</span>block<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">def</span> excerpt<span style="color:#006600; font-weight:bold;">&#40;</span>phrase, radius = <span style="color:#006666;">100</span>, excerpt_string = <span style="color:#996600;">&quot;...&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          TextHelperSingleton.<span style="color:#9900CC;">instance</span>.<span style="color:#9900CC;">excerpt</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>, phrase, radius, excerpt_string<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">def</span> highlight<span style="color:#006600; font-weight:bold;">&#40;</span>phrase, highlighter = <span style="color:#996600;">'&lt;strong class=&quot;highlight&quot;&gt;<span style="color:#000099;">\1</span>&lt;/strong&gt;'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          TextHelperSingleton.<span style="color:#9900CC;">instance</span>.<span style="color:#9900CC;">highlight</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>, phrase, highlighter<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">begin</span>
          require_library_or_gem <span style="color:#996600;">'bluecloth'</span>
&nbsp;
          <span style="color:#9966CC; font-weight:bold;">def</span> markdown
            TextHelperSingleton.<span style="color:#9900CC;">instance</span>.<span style="color:#9900CC;">markdown</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">LoadError</span>
          <span style="color:#008000; font-style:italic;"># do nothing.  method will be undefined</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">def</span> sanitize
          TextHelperSingleton.<span style="color:#9900CC;">instance</span>.<span style="color:#9900CC;">sanitize</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">def</span> simple_format
          TextHelperSingleton.<span style="color:#9900CC;">instance</span>.<span style="color:#9900CC;">simple_format</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">def</span> strip_tags
          TextHelperSingleton.<span style="color:#9900CC;">instance</span>.<span style="color:#9900CC;">strip_tags</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">begin</span>
          require_library_or_gem <span style="color:#996600;">'redcloth'</span>
&nbsp;
          <span style="color:#9966CC; font-weight:bold;">def</span> textilize
            TextHelperSingleton.<span style="color:#9900CC;">instance</span>.<span style="color:#9900CC;">textilize</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
          <span style="color:#9966CC; font-weight:bold;">def</span> textilize_without_paragraph
            TextHelperSingleton.<span style="color:#9900CC;">instance</span>.<span style="color:#9900CC;">textilize_without_paragraph</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">LoadError</span>
          <span style="color:#008000; font-style:italic;"># do nothing.  methods will be undefined</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">def</span> truncate<span style="color:#006600; font-weight:bold;">&#40;</span>length = <span style="color:#006666;">30</span>, truncate_string = <span style="color:#996600;">&quot;...&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          TextHelperSingleton.<span style="color:#9900CC;">instance</span>.<span style="color:#9900CC;">truncate</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>, length, truncate_string<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">def</span> word_wrap<span style="color:#006600; font-weight:bold;">&#40;</span>line_width = <span style="color:#006666;">80</span><span style="color:#006600; font-weight:bold;">&#41;</span>
          TextHelperSingleton.<span style="color:#9900CC;">instance</span>.<span style="color:#9900CC;">word_wrap</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span>, line_width<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># extend String with the TextHelper functions</span>
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">String</span> <span style="color:#008000; font-style:italic;">#:nodoc:</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">MyExtensions::CoreExtensions::<span style="color:#CC0066; font-weight:bold;">String</span>::TextHelper</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This idea and code was somewhat inspired by <a href="http://gabriel.gironda.org/articles/2006/02/08/using-helpers-inside-a-controller">Gabriel&#8217;s post on using helpers inside a controller</a>.  Thanks Gabriel!</p>
]]></content:encoded>
			<wfw:commentRss>http://csummers.com/2006/08/07/extend-string-to-use-actionviews-text-helpers/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

