<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:posterous="http://posterous.com/help/rss/1.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
  <channel>
    <title>Burke's posterous</title>
    <link>http://burkelibbey.posterous.com</link>
    <description>Most recent posts at Burke's posterous</description>
    <generator>posterous.com</generator>
    <link xmlns="http://www.w3.org/2005/Atom" type="application/json" href="http://posterous.com/api/sup_update#c9ecb04d7" rel="http://api.friendfeed.com/2008/03#sup" />
    
    
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/burkelibbey-posterous" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="burkelibbey-posterous" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://posterous.superfeedr.com/" /><item>
      <pubDate>Thu, 12 May 2011 13:33:00 -0700</pubDate>
      <title>Using Pry instead of IRB for Rails' console</title>
      <link>http://burkelibbey.posterous.com/using-pry-instead-of-irb-for-rails-console</link>
      <guid>http://burkelibbey.posterous.com/using-pry-instead-of-irb-for-rails-console</guid>
      <description>
        <![CDATA[<p>
	<p>Pry is a new-ish ruby gem that everyone should be using. Rather than me attempting to explain to you why it's so great, I suggest you skim <a href="http://rdoc.info/github/banister/pry/master/file/README.markdown">the README</a>. Highlights include "show-method", "edit-method", and almost everything else.</p>
<p>Rails doesn't make it particularly easy to use an "alternative" console, but if you stick "gem 'pry'" in your Gemfile, and paste the following code in an initializer, everything works just like magic.</p>
<div class="CodeRay">
  <div class="code"><pre><code>
# in config/initializers/pry.rb
begin
  require 'pry'
  module Rails
    class Console
      class IRB
        def self.start
          Pry.start
        end
      end 
    end
  end
rescue LoadError
end 

</code></pre></div>
</div>

<p>This is only tested in Rails 3.1. It should work in 3.0, but I don't think Rails::Console exists or works the same way in 2.3.x.</p>
	
</p>

<p><a href="http://burkelibbey.posterous.com/using-pry-instead-of-irb-for-rails-console">Permalink</a> 

	| <a href="http://burkelibbey.posterous.com/using-pry-instead-of-irb-for-rails-console#comment">Leave a comment&nbsp;&nbsp;&raquo;</a>

</p>]]>
      </description>
      <posterous:author>
        <posterous:userImage>http://files.posterous.com/user_profile_pics/157419/tophat.jpg</posterous:userImage>
        <posterous:profileUrl>http://posterous.com/users/4afy6B7Y2zPH</posterous:profileUrl>
        <posterous:firstName>Burke</posterous:firstName>
        <posterous:lastName>Libbey</posterous:lastName>
        <posterous:nickName>Burke</posterous:nickName>
        <posterous:displayName>Burke Libbey</posterous:displayName>
      </posterous:author>
    </item>
    <item>
      <pubDate>Fri, 21 Jan 2011 12:55:00 -0800</pubDate>
      <title>Opening Method Definitions from IRB</title>
      <link>http://burkelibbey.posterous.com/opening-method-definitions-from-irb</link>
      <guid>http://burkelibbey.posterous.com/opening-method-definitions-from-irb</guid>
      <description>
        <![CDATA[<p>
	<p>If you've spent any amount of time mucking around in irb to explore an API, you're familiar with the process. Run some code, go hunt for a method definition. Run some more code, go hunting again. I realized today that newer versions of ruby allow you to get the line number and file name of a method definition interactively, which led me to this handy ~/.irbrc snippet. You'll want to customize the third-to-last line a bit depending on what editor you use. If you're a textmate user, it'll probably just read "mate '#{x.__file__}'". Now, if you run something like "Fixnum.goto_instance_method('months')", your editor will open the ActiveSupport source at the definition of that method. Handy!</p>
<div class="CodeRay">
  <div class="code"><pre><code>
# Anywhere in ~/.irbrc...
class Object
  def goto_method(sym)
    __goto_method(method(sym))
  end 
  def goto_instance_method(sym)
    __goto_method(instance_method(sym))
  end 
  def __goto_method(x)
    system(&quot;$EDITOR -n '+#{x.__line__}' \&quot;#{x.__file__}\&quot;&quot;)
  end 
end 

</code></pre></div>
</div>
	
</p>

<p><a href="http://burkelibbey.posterous.com/opening-method-definitions-from-irb">Permalink</a> 

	| <a href="http://burkelibbey.posterous.com/opening-method-definitions-from-irb#comment">Leave a comment&nbsp;&nbsp;&raquo;</a>

</p>]]>
      </description>
      <posterous:author>
        <posterous:userImage>http://files.posterous.com/user_profile_pics/157419/tophat.jpg</posterous:userImage>
        <posterous:profileUrl>http://posterous.com/users/4afy6B7Y2zPH</posterous:profileUrl>
        <posterous:firstName>Burke</posterous:firstName>
        <posterous:lastName>Libbey</posterous:lastName>
        <posterous:nickName>Burke</posterous:nickName>
        <posterous:displayName>Burke Libbey</posterous:displayName>
      </posterous:author>
    </item>
    <item>
      <pubDate>Tue, 09 Feb 2010 17:15:00 -0800</pubDate>
      <title>Ruby's other BEGIN</title>
      <link>http://burkelibbey.posterous.com/rubys-other-begin</link>
      <guid>http://burkelibbey.posterous.com/rubys-other-begin</guid>
      <description>
        <![CDATA[<p>
	<p>No, I'm not talking about "begin", like begin/rescue/end begin. Ruby has a very, very strange feature that almost nobody knows about. It goes by the name of "BEGIN". Let's start with an example.</p>
<div class="CodeRay">
  <div class="code"><pre><code>
puts :first
BEGIN { puts :second }
# output:
#   second
#   first
</code></pre></div>
</div>

<p>Well that was unexpected! Even though "puts :first" should have been evaluated before the second line, the block we passed to BEGIN still somehow executed first. Let's try something else.</p>
<div class="CodeRay">
  <div class="code"><pre><code>
puts :first
BEGIN { puts :second }
puts :third
# output:
#   second
#   first
#   third
</code></pre></div>
</div>

<p>It seems that what's happening here is that the BEGIN block is evaluated first, no matter what the context. In fact, it's evaluated first even when it shouldn't normally be evaluated at all!</p>
<div class="CodeRay">
  <div class="code"><pre><code>
lambda { BEGIN { puts &quot;This lambda is never called.&quot; } }
# output:
#   This lambda is never called.
</code></pre></div>
</div>

<p>This seems extremely counter-intuitive; however, the explanation is quite simple. You can think of the BEGIN block like C's preprocessor. Anywhere a BEGIN block is encountered, it's automatically run before the rest of the file is executed.</p>
<p>Conversely, there's also an END block available, which runs _after_ all the other code.</p>
<div class="CodeRay">
  <div class="code"><pre><code>
lambda { END { puts :first } }
puts :second
# output:
#   second
</code></pre></div>
</div>

<p>And yet, END doesn't seem to follow the same rules as BEGIN. It doesn't trigger even from within code that's never executed. Why? The answer is actually pretty simple. To execute BEGIN before the rest of the file's contents, ruby has to do an initial pass to scan for it. Since executing the code twice is a bad idea for several reasons, the least of which is performance, it can't reliably determine whether a BEGIN block is actually evaluated, so assumes that if it appears, and isn't in a comment, it should be evalutated. END, on the other hand, can be detected as it's encountered during the normal execution of the program.</p>
<p>I haven't been able to come up with a valid use for BEGIN or END. The only thing I could think of -- using END to run some chunk of code after the program dies in a horrible fire -- doesn't work, since the END block isn't run if the program terminates prematurely. If you can think of anything useful, please leave a comment.</p>
	
</p>

<p><a href="http://burkelibbey.posterous.com/rubys-other-begin">Permalink</a> 

	| <a href="http://burkelibbey.posterous.com/rubys-other-begin#comment">Leave a comment&nbsp;&nbsp;&raquo;</a>

</p>]]>
      </description>
      <posterous:author>
        <posterous:userImage>http://files.posterous.com/user_profile_pics/157419/tophat.jpg</posterous:userImage>
        <posterous:profileUrl>http://posterous.com/users/4afy6B7Y2zPH</posterous:profileUrl>
        <posterous:firstName>Burke</posterous:firstName>
        <posterous:lastName>Libbey</posterous:lastName>
        <posterous:nickName>Burke</posterous:nickName>
        <posterous:displayName>Burke Libbey</posterous:displayName>
      </posterous:author>
    </item>
    <item>
      <pubDate>Mon, 08 Feb 2010 06:34:00 -0800</pubDate>
      <title>Rails 3 shell functions</title>
      <link>http://burkelibbey.posterous.com/rails-3-shell-functions</link>
      <guid>http://burkelibbey.posterous.com/rails-3-shell-functions</guid>
      <description>
        <![CDATA[<p>
	<p>If you've started a new project in Rails 3 yet, you've probably been a little thrown by the new `rails` executable that replaces all the old `script/*` scripts. I found a post this morning with a shell function to choose whichever one exists (<a href="http://metaskills.net/2010/2/6/simple-script-console-function">http://metaskills.net/2010/2/6/simple-script-console-function</a>). Here's my adaptation of it to search up through the directory structure until it finds a Rakefile. This lets you call, for example, `sgm create_users` in app/models.</p>
<div class="CodeRay">
  <div class="code"><pre><code>
find-rakefile () {
  if [ -f $1/Rakefile ]; then
    echo $1
  else 
    echo $(find-rakefile $1/..)
  fi
}

rails-script () {
  rfpath=$(find-rakefile .)
  target=$1; shift
  if [ -f $rfpath/script/rails ]; then 
    $rfpath/script/rails $target $argv
  else
    $rfpath/script/$target $argv
  fi
}

sc () { rails-script &quot;console&quot;  $argv }
ss () { rails-script &quot;server&quot;   $argv }
sg () { rails-script &quot;generate&quot; $argv }
sg () { rails-script &quot;runner&quot;   $argv }
sgm() { rails-script &quot;generate&quot; &quot;migration&quot; $argv }
sgs() { rails-script &quot;generate&quot; &quot;scaffold&quot;  $argv }
sgr() { rails-script &quot;generate&quot; &quot;resource&quot;  $argv }

</code></pre></div>
</div>
	
</p>

<p><a href="http://burkelibbey.posterous.com/rails-3-shell-functions">Permalink</a> 

	| <a href="http://burkelibbey.posterous.com/rails-3-shell-functions#comment">Leave a comment&nbsp;&nbsp;&raquo;</a>

</p>]]>
      </description>
      <posterous:author>
        <posterous:userImage>http://files.posterous.com/user_profile_pics/157419/tophat.jpg</posterous:userImage>
        <posterous:profileUrl>http://posterous.com/users/4afy6B7Y2zPH</posterous:profileUrl>
        <posterous:firstName>Burke</posterous:firstName>
        <posterous:lastName>Libbey</posterous:lastName>
        <posterous:nickName>Burke</posterous:nickName>
        <posterous:displayName>Burke Libbey</posterous:displayName>
      </posterous:author>
    </item>
    <item>
      <pubDate>Mon, 25 Jan 2010 19:46:00 -0800</pubDate>
      <title>andand and andand2</title>
      <link>http://burkelibbey.posterous.com/andand-and-andand2</link>
      <guid>http://burkelibbey.posterous.com/andand-and-andand2</guid>
      <description>
        <![CDATA[<p>
	<p>I've been using Raganwald's <tt>andand</tt> quite a bit at work lately. It saves a lot of hassle, and I fells more elegant than the <tt>#try(:sym)</tt> syntax.</p>
<p>Andand, for those who don't know, is a shortcut for the frequently-used <tt>object &amp;&amp; object.method</tt> pattern. You can instead write <tt>object.andand.method</tt>. In fact, you can even write <tt>object.andand.method1.method2.method3</tt>, which will just return <tt>nil</tt> instead of raising <tt>NoMethodError</tt> if <tt>method1</tt> returns <tt>nil</tt>. The major problem I have with <tt>andand</tt>, though, is that if <tt>method1</tt> returns non-nil, but <tt>method2</tt> returns <tt>nil</tt>, you still get an error. To protect the entire chain against <tt>NoMethodError</tt>s, you'd need to do <tt>object.andand.method1.andand.method2.andand.method3</tt>. Ugly.</p>
<p>While I realize this is out of <tt>andand</tt>'s original scope, it's something I find myself wanting to do quite a bit, so I wrote my own version of <tt>andand</tt> that propagates its protection a little more loosely. With my version, anything after <tt>andand</tt> in the method chain is pretty well incapable of generating a <tt>NoMethodError</tt>. For example:</p>
<h5>andand:</h5>
<p><code> "my_string".andand.size.to_f.to_s.gArBaGe.this_method_does_NOT_exist.size.foo.bar
 #=&gt; NoMethodError: undefined method `gArBaGe' for "9.0":String </code></p>
<h5>andand2:</h5>
<p><code> "my_string".andand.size.to_f.to_s.gArBaGe.this_method_does_NOT_exist.size.foo.bar
 #=&gt; nil </code></p>
<p>I find this extremely useful when chaining together large numbers of methods, when any number of them could return nil.&nbsp;</p>
<p>Check out the source at <a href="http://github.com/burke/andand2">http://github.com/burke/andand2</a>, or grab the gem "andand2" from gemcutter.</p>
	
</p>

<p><a href="http://burkelibbey.posterous.com/andand-and-andand2">Permalink</a> 

	| <a href="http://burkelibbey.posterous.com/andand-and-andand2#comment">Leave a comment&nbsp;&nbsp;&raquo;</a>

</p>]]>
      </description>
      <posterous:author>
        <posterous:userImage>http://files.posterous.com/user_profile_pics/157419/tophat.jpg</posterous:userImage>
        <posterous:profileUrl>http://posterous.com/users/4afy6B7Y2zPH</posterous:profileUrl>
        <posterous:firstName>Burke</posterous:firstName>
        <posterous:lastName>Libbey</posterous:lastName>
        <posterous:nickName>Burke</posterous:nickName>
        <posterous:displayName>Burke Libbey</posterous:displayName>
      </posterous:author>
    </item>
    <item>
      <pubDate>Mon, 14 Sep 2009 09:30:07 -0700</pubDate>
      <title>The Konami Code</title>
      <link>http://burkelibbey.posterous.com/the-konami-code</link>
      <guid>http://burkelibbey.posterous.com/the-konami-code</guid>
      <description>
        <![CDATA[<p>
	Here's a quick code snippet to redirect to a new page when the user <br />enters the Konami code on your website. Yeah, I know. Old news. I just <br />wanted to try out posterous' syntax highlighting. <p />&nbsp;<code> 
(function(){ 
&nbsp;var s=0,w=window,k=[38,38,40,40,37,39,37,39,66,65]; 
&nbsp;$(w).keydown(function(e){ 
&nbsp; if((e.keyCode==k[s]?++s:s=0)&gt;9)w.location="http://example.com/secret"; 
&nbsp;}); 
})(); 
</code>
	
</p>

<p><a href="http://burkelibbey.posterous.com/the-konami-code">Permalink</a> 

	| <a href="http://burkelibbey.posterous.com/the-konami-code#comment">Leave a comment&nbsp;&nbsp;&raquo;</a>

</p>]]>
      </description>
      <posterous:author>
        <posterous:userImage>http://files.posterous.com/user_profile_pics/157419/tophat.jpg</posterous:userImage>
        <posterous:profileUrl>http://posterous.com/users/4afy6B7Y2zPH</posterous:profileUrl>
        <posterous:firstName>Burke</posterous:firstName>
        <posterous:lastName>Libbey</posterous:lastName>
        <posterous:nickName>Burke</posterous:nickName>
        <posterous:displayName>Burke Libbey</posterous:displayName>
      </posterous:author>
    </item>
    <item>
      <pubDate>Sun, 06 Sep 2009 03:38:05 -0700</pubDate>
      <title>MonkeySupport: Monkeypatching Rails with C for significant 	performance increases</title>
      <link>http://burkelibbey.posterous.com/monkeysupport-monkeypatching-rails-with-c-for</link>
      <guid>http://burkelibbey.posterous.com/monkeysupport-monkeypatching-rails-with-c-for</guid>
      <description>
        <![CDATA[<p>
	MonkeySupport is a brand new gem I'm working on that should give rails <br />a significant performance boost. It's still in its infancy, but the <br />goal is to reimplement parts of rails in C, via the ruby C extension <br />API. Thus far, I've done a few of the methods in the <br />ActiveSupport::Inflector, such as camelize, underscore, demodulize, <br />and so on, with an average runtime decrease of about 75%. You can find <br />the code on github, at <a href="http://github.com/burke/monkeysupport">http://github.com/burke/monkeysupport</a> . <p />&nbsp;You're welcome to try MonkeySupport out. To use it in your rails app, <br />just add "config.gem 'burke-monkeysupport', :lib => 'monkeysupport', <br />:source => 'http://gems.github.com' " to config/environment.rb. You <br />definitely *don't* want to use it in production yet though. I haven't <br />bothered to figure out a way to run it against rails' test suite since <br />I pulled it out of my rails fork a few days ago. <p />&nbsp;The next thing I'm going to try to tackle for monkeysupport is html <br />helpers, like html_escape, link_to, image_tag, and so on. They're <br />called quite often, and string manipulation functions tend to be <br />easiest to rewrite in C. <p />&nbsp;There are a few ways you can help out if you want: If you're ambitious <br />enough to port some ruby to C yourself, just fork monkeysupport and <br />send me a pull request; I'd also love to hear any feedback, comments, <br />criticisms, etc. you have. Benchmark results, cryptic errors, random <br />segfaults -- all appreciated. <p />&nbsp;I'll get monkeysupport wired up to the rails test suite in the next <br />few days. releases 0.2.0 and onward will be fully tested. Stay tuned.
	
</p>

<p><a href="http://burkelibbey.posterous.com/monkeysupport-monkeypatching-rails-with-c-for">Permalink</a> 

	| <a href="http://burkelibbey.posterous.com/monkeysupport-monkeypatching-rails-with-c-for#comment">Leave a comment&nbsp;&nbsp;&raquo;</a>

</p>]]>
      </description>
      <posterous:author>
        <posterous:userImage>http://files.posterous.com/user_profile_pics/157419/tophat.jpg</posterous:userImage>
        <posterous:profileUrl>http://posterous.com/users/4afy6B7Y2zPH</posterous:profileUrl>
        <posterous:firstName>Burke</posterous:firstName>
        <posterous:lastName>Libbey</posterous:lastName>
        <posterous:nickName>Burke</posterous:nickName>
        <posterous:displayName>Burke Libbey</posterous:displayName>
      </posterous:author>
    </item>
    <item>
      <pubDate>Sun, 06 Sep 2009 03:08:02 -0700</pubDate>
      <title>Hello Posterous</title>
      <link>http://burkelibbey.posterous.com/hello-posterous-181</link>
      <guid>http://burkelibbey.posterous.com/hello-posterous-181</guid>
      <description>
        <![CDATA[<p>
	Testing, Hello World, etc.
	
</p>

<p><a href="http://burkelibbey.posterous.com/hello-posterous-181">Permalink</a> 

	| <a href="http://burkelibbey.posterous.com/hello-posterous-181#comment">Leave a comment&nbsp;&nbsp;&raquo;</a>

</p>]]>
      </description>
      <posterous:author>
        <posterous:userImage>http://files.posterous.com/user_profile_pics/157419/tophat.jpg</posterous:userImage>
        <posterous:profileUrl>http://posterous.com/users/4afy6B7Y2zPH</posterous:profileUrl>
        <posterous:firstName>Burke</posterous:firstName>
        <posterous:lastName>Libbey</posterous:lastName>
        <posterous:nickName>Burke</posterous:nickName>
        <posterous:displayName>Burke Libbey</posterous:displayName>
      </posterous:author>
    </item>
  </channel>
</rss>

