# Using irb as a REPL

**URL:** <https://www.ruby-forum.com/t/using-irb-as-a-repl/120706>\
**Category:** Ruby\
**Created:** [November 20, 2007, 12:26am UTC](https://www.ruby-forum.com/t/using-irb-as-a-repl/120706 "2007-11-20T00:26:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [November 20, 2007, 12:26am UTC](https://www.ruby-forum.com/t/using-irb-as-a-repl/120706/1 "2007-11-20T00:26:07Z")

</div>

Lisp programmers often use the REPL (similar to IRB) as a simple way  
to make an interactive interface to their app. I’d like to do the  
same thing with irb - that is, define my classes, and load irb in the  
context.

I can get about half way there:

require ‘irb’  
IRB.start  
my\_class = MyClass.new(…) # I want this to be the context  
irb my\_class

…works, but you have to type “quit” to load. Is there a better way  
to leverage IRB from within my code? What do the Ruby hackers say? (I  
know a lot have a Lisp background, and so wouldn’t want Ruby to not  
have a REPL shell…)

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [November 20, 2007, 12:35am UTC](https://www.ruby-forum.com/t/using-irb-as-a-repl/120706/2 "2007-11-20T00:35:28Z")

</div>

On Nov 19, 6:21 pm, “S. Robert J.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> irb my\_class
> 
> …works, but you have to type “quit” to load. Is there a better way  
> to leverage IRB from within my code? What do the Ruby hackers say? (I  
> know a lot have a Lisp background, and so wouldn’t want Ruby to not  
> have a REPL shell…)

Another, similar problem with the above:  
irb my\_class  
conf.prompt\_mode = :SIMPLE # This line is ignored

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [November 20, 2007, 3:08am UTC](https://www.ruby-forum.com/t/using-irb-as-a-repl/120706/3 "2007-11-20T03:08:32Z")

</div>

On 11/19/07, S. Robert J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > my\_class = MyClass.new(…) # I want this to be the context  
> > irb my\_class
> > 
> > …works, but you have to type “quit” to load. Is there a better way  
> > to leverage IRB from within my code? What do the Ruby hackers say? (I  
> > know a lot have a Lisp background, and so wouldn’t want Ruby to not  
> > have a REPL shell…)
> 
> Another, similar problem with the above:  
> irb my\_class  
> conf.prompt\_mode = :SIMPLE # This line is ignored

hmmm, I’ve also had some problems trying to add a command interface to  
some code. I found trying to bend IRB to fit the needs seems to be  
trickier than just coding up a solution in readline directly. Is that  
really the best solution?

Cameron

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [November 20, 2007, 3:36am UTC](https://www.ruby-forum.com/t/using-irb-as-a-repl/120706/4 "2007-11-20T03:36:01Z")

</div>

On Nov 19, 9:07 pm, Cameron McBride [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> hmmm, I’ve also had some problems trying to add a command interface to  
> some code. I found trying to bend IRB to fit the needs seems to be  
> trickier than just coding up a solution in readline directly. Is that  
> really the best solution?
> 
> Cameron

Then you miss out on the full power of Ruby in your interface!  
The beauty of using a REPL or IRB is that you have an easy command  
line interface, with automatic scripting when you need it, expandable  
to the full power of the host language as needed.

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [November 20, 2007, 3:48am UTC](https://www.ruby-forum.com/t/using-irb-as-a-repl/120706/5 "2007-11-20T03:48:18Z")

</div>

On 11/19/07, S. Robert J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The beauty of using a REPL or IRB is that you have an easy command  
> line interface, with automatic scripting when you need it, expandable  
> to the full power of the host language as needed.

Sorry, I wasn’t clear. I’m talking about building a ruby library and  
then the CLI with readline does a stateful eval. Problem is that it  
reimplements functionality that already exists in IRB.

Cameron

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [November 20, 2007, 4:43am UTC](https://www.ruby-forum.com/t/using-irb-as-a-repl/120706/6 "2007-11-20T04:43:14Z")

</div>

On Tue, Nov 20, 2007 at 08:25:00AM +0900, S. Robert J. wrote:

> Lisp programmers often use the REPL (similar to IRB) as a simple way  
> to make an interactive interface to their app. I’d like to do the  
> same thing with irb - that is, define my classes, and load irb in the  
> context.

There’s an example of this in Shoes:

[http://code.whytheluckystiff.net/svn/shoes/trunk/samples/irb.rb](http://code.whytheluckystiff.net/svn/shoes/trunk/samples/irb.rb)

The MimickIRB class just wraps IRB’s RubyLex class in a very simple  
REPL. I don’t know if this is exactly what you’re looking for  
(since it doesn’t do debugging, .irbrc, or frames) but it’s nice if  
you want to start your own from nothing.

\_why

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [November 20, 2007, 9:08pm UTC](https://www.ruby-forum.com/t/using-irb-as-a-repl/120706/7 "2007-11-20T21:08:35Z")

</div>

On 11/19/07, S. Robert J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Lisp programmers often use the REPL (similar to IRB) as a simple way  
> to make an interactive interface to their app. I’d like to do the  
> same thing with irb - that is, define my classes, and load irb in the  
> context.

There’s one very simple way to do this. Do

gem install aws-s3

And have a look at s3sh in the bin dir. It’s basically just a shell  
script. It just aims regular old IRB at a setup file which establishes  
some relevant variables and loads some relevant modules.

Rails console does nearly exactly the same thing - all it really adds  
in is more libraries and some command-line args with optparse.

–  
Giles B.

Podcast: [http://hollywoodgrit.blogspot.com](http://hollywoodgrit.blogspot.com)  
Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com](http://giles.tumblr.com)

---

<div class="post-metadata">

**Author:** ![robertjames](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9c0ed/32.png) [@robertjames](https://www.ruby-forum.com/u/robertjames)\
**Post date:** [November 29, 2007, 10:46pm UTC](https://www.ruby-forum.com/t/using-irb-as-a-repl/120706/8 "2007-11-29T22:46:39Z")

</div>

S. Robert J. wrote:

> irb my\_class
> 
> …works, but you have to type “quit” to load. Is there a better way  
> to leverage IRB from within my code? What do the Ruby hackers say? (I  
> know a lot have a Lisp background, and so wouldn’t want Ruby to not  
> have a REPL shell…)

This might help:

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/244139](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/244139)
