# Thoughts on rewriting irb?

**URL:** <https://www.ruby-forum.com/t/thoughts-on-rewriting-irb/172598>\
**Category:** Ruby\
**Created:** [July 23, 2009, 8:50pm UTC](https://www.ruby-forum.com/t/thoughts-on-rewriting-irb/172598 "2009-07-23T20:50:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ghorner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a183cd/32.png) [@ghorner](https://www.ruby-forum.com/u/ghorner)\
**Post date:** [July 23, 2009, 8:50pm UTC](https://www.ruby-forum.com/t/thoughts-on-rewriting-irb/172598/1 "2009-07-23T20:50:56Z")

</div>

Hi,  
I wrote up a post on a mini irb which does better irb completion,  
persistent history and error handling in 7 lines:

> **[Tagaholic - Mini Irb and Mini Script/Console](http://tagaholic.me/2009/07/23/mini-irb-and-mini-script-console.html)**
>
> In my last post, I introduced Bond's own version of irb's completion. What I didn't emphasize is that it doesn't need irb. To prove it, I'll show you a mini irb which has persi...

If you’re curious the code looks like this:  
%w{readline rubygems bond bond/completion}.each {|e| require e }  
history\_file = File.join(ENV[“HOME”], ‘.mini\_irb\_history’)  
IO.readlines(history\_file).each {|e| Readline::HISTORY \<\< e.chomp } if  
File.exists?(history\_file)  
while (input = Readline.readline('\>\> ', true)) != ‘exit’  
begin puts “=\> #{eval(input).inspect}”; rescue Exception; puts  
“Error: #{$!}” end  
end  
File.open(history\_file, ‘w’) {|f| f.write Readline::HISTORY.to\_a.join  
(“\n”) }

Since that probably came out horribly, you could also just see this  
snippet: [Can you make a smaller one? · GitHub](http://gist.github.com/68608)

At first, it may seem like a joke to compare it to irb but imho it  
does the brunt of what a repl should do. All of irb’s features:  
workspaces, jobs, config options, command framework are features which  
the majority of ruby programmers don’t seem to use. Personally the  
only thing I’d port to mini-irb is a ruby lexer to detect multiple  
lines of ruby. So what are other people’s two cents? If you could  
rewrite irb, what features would you give it?

---

<div class="post-metadata">

**Author:** ![ghorner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a183cd/32.png) [@ghorner](https://www.ruby-forum.com/u/ghorner)\
**Post date:** [July 24, 2009, 3:48am UTC](https://www.ruby-forum.com/t/thoughts-on-rewriting-irb/172598/2 "2009-07-24T03:48:16Z")

</div>

ghorner wrote:

> begin puts “=\> #{eval(input).inspect}”; rescue Exception; puts

Do you want

eval(input, b)

where b is a Binding that was constructed before the loop? Otherwise,  
how will this support local vars?

---

<div class="post-metadata">

**Author:** ![ghorner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a183cd/32.png) [@ghorner](https://www.ruby-forum.com/u/ghorner)\
**Post date:** [July 24, 2009, 6:17am UTC](https://www.ruby-forum.com/t/thoughts-on-rewriting-irb/172598/3 "2009-07-24T06:17:16Z")

</div>

Hi,

Am Freitag, 24. Jul 2009, 03:50:06 +0900 schrieb ghorner:

> I wrote up a post on a mini irb which does better irb completion,  
> persistent history and error handling in 7 lines:  
> [Tagaholic - Mini Irb and Mini Script/Console](http://tagaholic.me/2009/07/23/mini-irb-and-mini-script-console.html)
> 
> If you’re curious the code looks like this:  
> […]

I’m very disappointed about Irb. The thing I miss most is some way  
to say “Examine the object now” while debugging. To be honest I  
wrote me my own interactive Ruby and I didn’t use Irb for about a  
year. I would like to publish my one but the thing leaving me  
dissatified is the Readline library. A real good command line  
library would allow me to do things like entering my favourite  
editor, be able to yield coloured output at the prompt and maybe  
the entered line, a configurable history management, and much  
more. The objects environment sould be left untouched; for example  
the “exit” method shouldn’t be redefined, rather something like  
the PostgreSQL \q command should replace it.

I would like to share my approach for further development.

Bertram

---

<div class="post-metadata">

**Author:** ![ghorner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a183cd/32.png) [@ghorner](https://www.ruby-forum.com/u/ghorner)\
**Post date:** [July 24, 2009, 8:35am UTC](https://www.ruby-forum.com/t/thoughts-on-rewriting-irb/172598/4 "2009-07-24T08:35:45Z")

</div>

On Jul 23, 9:47 pm, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ghorner wrote:
> 
> > begin puts “=\> #{eval(input).inspect}”; rescue Exception; puts
> 
> Do you want
> 
> eval(input, b)
> 
> where b is a Binding that was constructed before the loop? Otherwise,  
> how will this support local vars?

Joel,  
not sure I follow. In mini-irb this works:

> > b = 2  
> > =\> 2  
> > ‘b’ \* b  
> > =\> “bb”

As does placing local variables before the loop being picked up by  
eval. Could you elaborate?  
Gabriel

---

<div class="post-metadata">

**Author:** ![ghorner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a183cd/32.png) [@ghorner](https://www.ruby-forum.com/u/ghorner)\
**Post date:** [July 25, 2009, 12:40am UTC](https://www.ruby-forum.com/t/thoughts-on-rewriting-irb/172598/5 "2009-07-25T00:40:38Z")

</div>

cldwalker wrote:

> Joel,

Here’s a simplified version of your code:

$ cat mirb.rb  
%w{readline rubygems}.each {|e| require e }  
while (input = Readline.readline(’\>\> ', true)) != ‘exit’  
begin puts “=\> #{eval(input).inspect}”; rescue Exception; puts  
“Error: #{$!}” end  
end  
$ ruby mirb.rb

> > input  
> > =\> “input”

This is fixable using bindings:

$ cat mirb.rb  
%w{readline rubygems}.each {|e| require e }  
def get\_binding; binding; end  
b = get\_binding  
while (input = Readline.readline(’\>\> ', true)) != ‘exit’  
begin puts “=\> #{eval(input, b).inspect}”; rescue Exception; puts  
“Error: #{$!}” end  
end  
$ ruby mirb.rb

> > input

> >

(Now, some might say that’s still a bug, but at least it doesn’t expose  
a local variable of mini-irb.)

---

<div class="post-metadata">

**Author:** ![ghorner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a183cd/32.png) [@ghorner](https://www.ruby-forum.com/u/ghorner)\
**Post date:** [July 25, 2009, 1:50pm UTC](https://www.ruby-forum.com/t/thoughts-on-rewriting-irb/172598/6 "2009-07-25T13:50:45Z")

</div>

On Jul 24, 6:36 pm, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Gabriel
> > 
> > > input  
> > > “Error: #{$!}” end  
> > > end  
> > > $ ruby mirb.rb  
> > > input
> 
> > >
> 
> (Now, some might say that’s still a bug, but at least it doesn’t expose  
> a local variable of mini-irb.)

Thanks for pointing that out. Would you know why irb does some funkier  
binding with its IRB.conf[:CONTEXT\_MODE] option i.e. binding to a  
temporary file? I asked about it here,  
[http://groups.google.com/group/comp.lang.ruby/browse\_thread/thread/6aa102dba71850f3](http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/6aa102dba71850f3),  
but no one seemed to know.  
Now with proper binding, mini-irb stands at less than 10 lines. So I’m  
still asking, what of the 5000+ loc in irb do people actually use?

---

<div class="post-metadata">

**Author:** ![ghorner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a183cd/32.png) [@ghorner](https://www.ruby-forum.com/u/ghorner)\
**Post date:** [July 24, 2009, 8:50am UTC](https://www.ruby-forum.com/t/thoughts-on-rewriting-irb/172598/7 "2009-07-24T08:50:57Z")

</div>

On Jul 24, 12:16 am, Bertram S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I’m very disappointed about Irb. The thing I miss most is some way  
> to say “Examine the object now” while debugging. To be honest I

Ditto on the disappointment. You can use breakpoint for examining an  
object at any point of your program.

> dissatified is the Readline library. A real good command line  
> library would allow me to do things like entering my favourite  
> editor, be able to yield coloured output at the prompt and maybe  
> the entered line, a configurable history management, and much

Can’t you enter what you need to via “system ‘myeditor’”? Colored  
output is  
done ok with wirble. Readline::HISTORY can be as configurable as you  
make it. Here’s  
my own history management module:

> <https://github.com/cldwalker/irbfiles/blob/6e5c6878ec2f3ca6f29c4751f64682e30af555b8/.irb/libraries/history.rb>

> more. The objects environment sould be left untouched; for example  
> the “exit” method shouldn’t be redefined, rather something like  
> the PostgreSQL \q command should replace it.

With the 7-liner in this post you could change ‘exit’ to ‘\q’.

> I would like to share my approach for further development.  
> Share more if you’d like.

Gabriel
