Nick Sieger: Tweaking IRB http://blog.nicksieger.com/articles/2006/04/23/tweaking-irb en-us 40 "Tweaking IRB" by Rand Thacker <p>You can also use HOMEPATH which should be defined on XP pro (not sure about XP home)</p> <p>just change LOG = &#8220;#{ENV[&#8216;HOME&#8217;]}/.irb-history&#8221; to LOG = &#8220;#{ENV[&#8216;HOMEPATH&#8217;]}.irb-history&#8221;</p> <p>Rand</p> Wed, 26 Jul 2006 21:29:59 +0000 urn:uuid:f6fe6472-c4d4-466b-827d-46c3186b28e6 http://blog.nicksieger.com/articles/2006/04/23/tweaking-irb#comment-36 "Tweaking IRB" by Jessper Rønn-Jensen (justaddwater.dk) <p>Thanks a lot for this Nick! What a great trick. Thanks a lot for sharing.</p> <p>I had to make one small adjustment compared to what you describe above. On my system (win xp), I had to set a system variable &#8220;HOME&#8221; and point it to a dir where I put the .irbrc file.</p> <p>Found via: &#8221;<a href='http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/77139' rel="nofollow">irb completion under windows</a>&#8221; (ruby-talk)</p> <p>Please let me (and your other readers) know of any updates you make to this code.</p> <p>/Jesper</p> <p><a href='http://justaddwater.dk' rel="nofollow">http://justaddwater.dk</a></p> Wed, 26 Jul 2006 21:20:48 +0000 urn:uuid:e7569aef-d22a-44f3-b075-9bb23dbc2a00 http://blog.nicksieger.com/articles/2006/04/23/tweaking-irb#comment-35 Tweaking IRB <p>IRB (Interactive Ruby Shell) is one of those tools that a hacker learning Ruby hopefully discovers right away. It&#8217;s an extremely useful way to learn the language, verify hunches, test assumptions, and get immediate feedback. IRB promotes learning by doing, which is the best way of making something stick in your head. (You can even <a href="http://tryruby.hobix.com/">try an online version of irb</a> without even installing Ruby!)</p> <p>The first order of business when using IRB is to setup your preferences. If you haven&#8217;t done so already, create the file <code>~/.irbrc</code> (<code>%USERPROFILE%.irbrc</code> on windows native ruby). <code>.irbrc</code> is just a regular ruby script where you can run arbitrary ruby code at the start of your IRB session. Add the following to <code>.irbrc</code>: </p> <div class="typocode"><pre><code class="typocode_ruby "><span class="ident">require</span> <span class="punct">'</span><span class="string">irb/completion</span><span class="punct">'</span> <span class="constant">ARGV</span><span class="punct">.</span><span class="ident">concat</span> <span class="punct">[</span> <span class="punct">&quot;</span><span class="string">--readline</span><span class="punct">&quot;,</span> <span class="punct">&quot;</span><span class="string">--prompt-mode</span><span class="punct">&quot;,</span> <span class="punct">&quot;</span><span class="string">simple</span><span class="punct">&quot;</span> <span class="punct">]</span></code></pre></div> <p>This sets up usage of <a href="http://tiswww.tis.case.edu/~chet/readline/rltop.html">readline</a> in your session and turns on TAB completion, making IRB feel as comfortable as regular old <code>bash</code>. Now you can type <code>Kernel::&lt;TAB&gt;</code> and get a list of available methods! Good.</p> <p>Next, the thing that you find yourself doing after using IRB for a while is cutting and pasting code from your console buffer over to your text editor. Don&#8217;t have Ruby&#8217;s reflection rules down yet? Not sure whether to use <code>instance_eval</code> or <code>module_eval</code> when working on that metaprogramming hack? Working inside Rails&#8217; <code>script/console</code> and searching for the right ActiveRecord finder options? No matter how good your terminal program, you probably have to use the mouse to select text out of it to copy to your text editor, and hackers hate having to switch from the keyboard to the mouse when in the flow of programming.</p> <p>So here&#8217;s a technique that will append commands entered in your IRB session to a file in your home directory (idea from <a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/58931">ruby-talk:58931</a>). Put the following in your <code>.irbrc</code>:</p> <div class="typocode"><pre><code class="typocode_ruby "><span class="keyword">module </span><span class="module">Readline</span> <span class="keyword">module </span><span class="module">History</span> <span class="constant">LOG</span> <span class="punct">=</span> <span class="punct">&quot;</span><span class="string"><span class="expr">#{ENV['HOME']}</span>/.irb-history</span><span class="punct">&quot;</span> <span class="keyword">def </span><span class="method">self.write_log</span><span class="punct">(</span><span class="ident">line</span><span class="punct">)</span> <span class="constant">File</span><span class="punct">.</span><span class="ident">open</span><span class="punct">(</span><span class="constant">LOG</span><span class="punct">,</span> <span class="punct">'</span><span class="string">ab</span><span class="punct">')</span> <span class="punct">{|</span><span class="ident">f</span><span class="punct">|</span> <span class="ident">f</span> <span class="punct">&lt;&lt;</span> <span class="punct">&quot;</span><span class="string"><span class="expr">#{line}</span> </span><span class="punct">&quot;}</span> <span class="keyword">end</span> <span class="keyword">def </span><span class="method">self.start_session_log</span> <span class="ident">write_log</span><span class="punct">(&quot;</span><span class="string"> # session start: <span class="expr">#{Time.now}</span> </span><span class="punct">&quot;)</span> <span class="ident">at_exit</span> <span class="punct">{</span> <span class="ident">write_log</span><span class="punct">(&quot;</span><span class="string"> # session stop: <span class="expr">#{Time.now}</span> </span><span class="punct">&quot;)</span> <span class="punct">}</span> <span class="keyword">end</span> <span class="keyword">end</span> <span class="keyword">alias</span> <span class="symbol">:old_readline</span> <span class="symbol">:readline</span> <span class="keyword">def </span><span class="method">readline</span><span class="punct">(*</span><span class="ident">args</span><span class="punct">)</span> <span class="ident">ln</span> <span class="punct">=</span> <span class="ident">old_readline</span><span class="punct">(*</span><span class="ident">args</span><span class="punct">)</span> <span class="keyword">begin</span> <span class="constant">History</span><span class="punct">.</span><span class="ident">write_log</span><span class="punct">(</span><span class="ident">ln</span><span class="punct">)</span> <span class="keyword">rescue</span> <span class="keyword">end</span> <span class="ident">ln</span> <span class="keyword">end</span> <span class="keyword">end</span> <span class="constant">Readline</span><span class="punct">::</span><span class="constant">History</span><span class="punct">.</span><span class="ident">start_session_log</span></code></pre></div> <p>Now every line typed into IRB will immediately be saved into <code>~/.irb-history</code>. Exercise left to the reader to bind a custom keystroke and macro to yank the last line out of that file and automatically paste into your text editor.</p> <p>Long-time <code>bash</code> users know that the shell maintains a history of commands across sessions so that you can access commands you typed yesterday. Wouldn&#8217;t it be nice to do this in IRB as well? Wish granted:</p> <div class="typocode"><pre><code class="typocode_ruby "><span class="ident">require</span> <span class="punct">'</span><span class="string">irb/ext/save-history</span><span class="punct">'</span> <span class="constant">IRB</span><span class="punct">.</span><span class="ident">conf</span><span class="punct">[</span><span class="symbol">:SAVE_HISTORY</span><span class="punct">]</span> <span class="punct">=</span> <span class="number">100</span> <span class="constant">IRB</span><span class="punct">.</span><span class="ident">conf</span><span class="punct">[</span><span class="symbol">:HISTORY_FILE</span><span class="punct">]</span> <span class="punct">=</span> <span class="punct">&quot;</span><span class="string"><span class="expr">#{ENV['HOME']}</span>/.irb-save-history</span><span class="punct">&quot;</span></code></pre></div> <p>Happy ruby hacking! If you find any more handy IRB tips leave them at <a href="http://www.rubygarden.org/ruby?Irb/TipsAndTricks">rubygarden</a>, and let me know about them.</p> <p><em>Footnote: I realize there is duplication and non-DRY happening here with two copies of your IRB history, but I came across these techniques at two different times, and the functions they serve seem different enough to potentially use them both. If you don&#8217;t like that, choose whichever is more appropriate for your needs.</em></p> <p><em>(Hope this post serves your needs <a href="http://www.unpossible.com/2006/04/20/time-saver-rails-console-reset">Dan</a>.)</em></p> Sun, 23 Apr 2006 04:02:00 +0000 urn:uuid:5600581f-3980-43d5-897f-764d25149abb Nick Sieger http://blog.nicksieger.com/articles/2006/04/23/tweaking-irb ruby http://blog.nicksieger.com/articles/trackback/2