Nick Sieger: Tweaking IRBtag:blog.nicksieger.com,2005:TypoTypo2007-08-31T16:36:30+00:00Rand Thackerurn:uuid:f6fe6472-c4d4-466b-827d-46c3186b28e62006-07-26T21:29:59+00:002007-08-31T16:36:30+00:00Comment on 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 = “#{ENV[‘HOME’]}/.irb-history”
to
LOG = “#{ENV[‘HOMEPATH’]}.irb-history”</p>
<p>Rand</p>Jessper Rønn-Jensen (justaddwater.dk)urn:uuid:e7569aef-d22a-44f3-b075-9bb23dbc2a002006-07-26T21:20:48+00:002007-08-31T16:36:30+00:00Comment on 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 “HOME” and point it to a dir where I put the .irbrc file.</p>
<p>Found via: ”<a href='http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/77139' rel="nofollow">irb completion under windows</a>” (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>Nick Siegerurn:uuid:5600581f-3980-43d5-897f-764d25149abb2006-04-23T04:02:00+00:002007-08-31T16:36:30+00:00Tweaking IRB<p>IRB (Interactive Ruby Shell) is one of those tools that a hacker
learning Ruby hopefully discovers right away. It’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’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">"</span><span class="string">--readline</span><span class="punct">",</span> <span class="punct">"</span><span class="string">--prompt-mode</span><span class="punct">",</span> <span class="punct">"</span><span class="string">simple</span><span class="punct">"</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::<TAB></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’t have Ruby’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’ <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’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">"</span><span class="string"><span class="expr">#{ENV['HOME']}</span>/.irb-history</span><span class="punct">"</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"><<</span> <span class="punct">"</span><span class="string"><span class="expr">#{line}</span>
</span><span class="punct">"}</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">("</span><span class="string">
# session start: <span class="expr">#{Time.now}</span>
</span><span class="punct">")</span>
<span class="ident">at_exit</span> <span class="punct">{</span> <span class="ident">write_log</span><span class="punct">("</span><span class="string">
# session stop: <span class="expr">#{Time.now}</span>
</span><span class="punct">")</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’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">"</span><span class="string"><span class="expr">#{ENV['HOME']}</span>/.irb-save-history</span><span class="punct">"</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’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>