<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:refactormycode.com,2007:users255</id>
  <link type="application/atom+xml" href="http://refactormycode.com/users/255" rel="self"/>
  <title>webmat</title>
  <updated>Tue Oct 30 14:50:16 +0000 2007</updated>
  <entry>
    <id>tag:refactormycode.com,2007:Refactor649</id>
    <published>2007-10-30T14:50:16+00:00</published>
    <title>[Ruby] On String#to_proc (by Reginald Braithwaite)</title>
    <content type="html">&lt;p&gt;Nice! 
&lt;br /&gt;I should post the rspec tests too, to update them (they're on the original article). I'll do that _after_ my presentation at MontrealonRails next tuesday, though. I'm busy enough preparing for that ;-)&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>webmat</name>
      <email>webmat@gmail.com</email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/114-string-to_proc-by-reginald-braithwaite/refactors/649" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:refactormycode.com,2007:Code114</id>
    <published>2007-10-28T06:27:58+00:00</published>
    <updated>2007-10-30T14:50:18+00:00</updated>
    <title>[Ruby] String#to_proc (by Reginald Braithwaite)</title>
    <content type="html">&lt;p&gt;Reginald Braithwaite has a very interesting article on his blog at &lt;a href="http://weblog.raganwald.com/2007/10/stringtoproc.html" target="_blank"&gt;http://weblog.raganwald.com/2007/10/stringtoproc.html&lt;/a&gt; where he presents a port for Ruby of String Lambdas from Oliver Steele&#8217;s Functional Javascript. It's exceedingly cool :-)
&lt;br /&gt;Go read the article to see what it does first.
&lt;br /&gt;I thought I could post it here when I saw Giles Bowkett used pastie to submit a suggestion.&lt;/p&gt;

&lt;pre&gt;# String#to_proc
#
# See http://weblog.raganwald.com/2007/10/stringtoproc.html ( Subscribe in a reader)
#
# Ported from the String Lambdas in Oliver Steele's Functional Javascript
# http://osteele.com/sources/javascript/functional/
#
# This work is licensed under the MIT License:
#
# (c) 2007 Reginald Braithwaite
# Portions Copyright (c) 2006 Oliver Steele
# 
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# &amp;quot;Software&amp;quot;), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
# 
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED &amp;quot;AS IS&amp;quot;, WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

class String
  unless ''.respond_to?(:to_proc)
    def to_proc &amp;amp;block
      params = []
      expr = self
      sections = expr.split(/\s*-&amp;gt;\s*/m)
      if sections.length &amp;gt; 1 then
          eval sections.reverse!.inject { |e, p| &amp;quot;(Proc.new { |#{p.split(/\s/).join(', ')}| #{e} })&amp;quot; }, block &amp;amp;&amp;amp; block.binding
      elsif expr.match(/\b_\b/)
          eval &amp;quot;Proc.new { |_| #{expr} }&amp;quot;, block &amp;amp;&amp;amp; block.binding
      else
          leftSection = expr.match(/^\s*(?:[+*\/%&amp;amp;|\^\.=&amp;lt;&amp;gt;\[]|!=)/m)
          rightSection = expr.match(/[+\-*\/%&amp;amp;|\^\.=&amp;lt;&amp;gt;!]\s*$/m)
          if leftSection || rightSection then
              if (leftSection) then
                  params.push('$left')
                  expr = '$left' + expr
              end
              if (rightSection) then
                  params.push('$right')
                  expr = expr + '$right'
              end
          else
              self.gsub(
                  /(?:\b[A-Z]|\.[a-zA-Z_$])[a-zA-Z_$\d]*|[a-zA-Z_$][a-zA-Z_$\d]*:|self|arguments|'(?:[^'\\]|\\.)*'|&amp;quot;(?:[^&amp;quot;\\]|\\.)*&amp;quot;/, ''
              ).scan(
                /([a-z_$][a-z_$\d]*)/i
              ) do |v|  
                params.push(v) unless params.include?(v)
              end
          end
          eval &amp;quot;Proc.new { |#{params.join(', ')}| #{expr} }&amp;quot;, block &amp;amp;&amp;amp; block.binding
      end
    end
  end
end&lt;/pre&gt;</content>
    <author>
      <name>webmat</name>
      <email>webmat@gmail.com</email>
    </author>
    <link type="text/html" href="http://refactormycode.com/codes/114-string-to_proc-by-reginald-braithwaite" rel="alternate"/>
  </entry>
</feed>
