# Calling Irb from inside an application

**URL:** <https://rubytalk.org/t/calling-irb-from-inside-an-application/43281>\
**Category:** ruby-talk\
**Created:** [25 December 2007 13:08 UTC](https://rubytalk.org/t/calling-irb-from-inside-an-application/43281 "2007-12-25T13:08:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [25 December 2007 13:08 UTC](https://rubytalk.org/t/calling-irb-from-inside-an-application/43281/1 "2007-12-25T13:08:41Z")

</div>

Hi,

this may be easy to anwer but it is difficult to google for.

While developping I would like to play around with objects from  
time to time. Just like this:

&nbsp;&nbsp;&nbsp;&nbsp;irb(main):001:0\> class C ; def f ; "F" ; end ; end  
&nbsp;&nbsp;&nbsp;&nbsp;=\> nil  
&nbsp;&nbsp;&nbsp;&nbsp;irb(main):002:0\> irb C.new  
&nbsp;&nbsp;&nbsp;&nbsp;irb#1(#\<C:0xb7910d40\>):001:0\> f  
&nbsp;&nbsp;&nbsp;&nbsp;=\> "F"  
&nbsp;&nbsp;&nbsp;&nbsp;irb#1(#\<C:0xb7910d40\>):002:0\>

In common my classes are not so easy to type in. So, I would like  
to call Irb from somewhere inside the application as I do here:

&nbsp;&nbsp;&nbsp;&nbsp;----\>-call\_irb.rb---------------  
&nbsp;&nbsp;&nbsp;&nbsp;#!/usr/bin/ruby

&nbsp;&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def f  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"F"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;if $0 == \_\_FILE\_\_ then  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require "irb"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c = C.new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IRB.start  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;----\<---------------------------

and then

&nbsp;&nbsp;&nbsp;&nbsp;user@host $ ./call\_irb.rb  
&nbsp;&nbsp;&nbsp;&nbsp;irb(main):001:0\> irb $c  
&nbsp;&nbsp;&nbsp;&nbsp;irb#1(#\<C:0xb7be4ee0\>):001:0\> f  
&nbsp;&nbsp;&nbsp;&nbsp;=\> "F"  
&nbsp;&nbsp;&nbsp;&nbsp;irb#1(#\<C:0xb7be4ee0\>):002:0\>

Is there a way how I can call C.new's Irb directly without doing  
the long-winded definition of a global variable first? I already  
examined the irb sources but this seems to be well-hidden to me.

Thanks in advance and Merry Christmas,

Bertram

> **···**
>
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [25 December 2007 13:26 UTC](https://rubytalk.org/t/calling-irb-from-inside-an-application/43281/2 "2007-12-25T13:26:10Z")

</div>

Alle martedì 25 dicembre 2007, Bertram Scharpf ha scritto:

> Hi,
> 
> this may be easy to anwer but it is difficult to google for.
> 
> While developping I would like to play around with objects from  
> time to time. Just like this:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;irb(main):001:0\> class C ; def f ; "F" ; end ; end  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> nil  
> &nbsp;&nbsp;&nbsp;&nbsp;irb(main):002:0\> irb C.new  
> &nbsp;&nbsp;&nbsp;&nbsp;irb#1(#\<C:0xb7910d40\>):001:0\> f  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> "F"  
> &nbsp;&nbsp;&nbsp;&nbsp;irb#1(#\<C:0xb7910d40\>):002:0\>
> 
> In common my classes are not so easy to type in. So, I would like  
> to call Irb from somewhere inside the application as I do here:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;----\>-call\_irb.rb---------------  
> &nbsp;&nbsp;&nbsp;&nbsp;#!/usr/bin/ruby
> 
> &nbsp;&nbsp;&nbsp;&nbsp;class C  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def f  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"F"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;&nbsp;if $0 == \_\_FILE\_\_ then  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require "irb"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c = C.new  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IRB.start  
> &nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;----\<---------------------------
> 
> and then
> 
> &nbsp;&nbsp;&nbsp;&nbsp;user@host $ ./call\_irb.rb  
> &nbsp;&nbsp;&nbsp;&nbsp;irb(main):001:0\> irb $c  
> &nbsp;&nbsp;&nbsp;&nbsp;irb#1(#\<C:0xb7be4ee0\>):001:0\> f  
> &nbsp;&nbsp;&nbsp;&nbsp;=\> "F"  
> &nbsp;&nbsp;&nbsp;&nbsp;irb#1(#\<C:0xb7be4ee0\>):002:0\>
> 
> Is there a way how I can call C.new's Irb directly without doing  
> the long-winded definition of a global variable first? I already  
> examined the irb sources but this seems to be well-hidden to me.
> 
> Thanks in advance and Merry Christmas,
> 
> Bertram

Not a direct answer to your question, but can't you load the file with the  
definition of the class in irb?

Stefano

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [25 December 2007 20:16 UTC](https://rubytalk.org/t/calling-irb-from-inside-an-application/43281/3 "2007-12-25T20:16:03Z")

</div>

> In common my classes are not so easy to type in. So, I would like  
> to call Irb from somewhere inside the application as I do here:
> 
> &nbsp;&nbsp;&nbsp;if $0 == \_\_FILE\_\_ then  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require "irb"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c = C.new  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IRB.start  
> &nbsp;&nbsp;&nbsp;end

I'm a bit confused. Your subject line IS handled by this code. Indeed, this is what I grabbed from some of my code when I read the subject line:

&nbsp;&nbsp;&nbsp;def explore  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object.const\_set :"G", self  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require 'irb'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Your grammar is in the constant G"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IRB.start(\_\_FILE\_\_)  
&nbsp;&nbsp;&nbsp;end

So, what is the actual subject of this question? It doesn't seem to be "long-windedness" (below) either.

> and then
> 
> &nbsp;&nbsp;&nbsp;user@host $ ./call\_irb.rb  
> &nbsp;&nbsp;&nbsp;irb(main):001:0\> irb $c  
> &nbsp;&nbsp;&nbsp;irb#1(#\<C:0xb7be4ee0\>):001:0\> f  
> &nbsp;&nbsp;&nbsp;=\> "F"  
> &nbsp;&nbsp;&nbsp;irb#1(#\<C:0xb7be4ee0\>):002:0\>
> 
> Is there a way how I can call C.new's Irb directly without doing  
> the long-winded definition of a global variable first? I already  
> examined the irb sources but this seems to be well-hidden to me.

I'm confused and havet o assume the question is a bit vague. How is "$c = " long winded? What do you actually want to know?

> **···**
>
> On Dec 25, 2007, at 05:08 , Bertram Scharpf wrote:

---

<div class="post-metadata">

**Author:** ![James\_Tucker](https://avatars.discourse-cdn.com/v4/letter/j/ccd318/32.png) [@James\_Tucker](https://rubytalk.org/u/James_Tucker)\
**Post date:** [25 December 2007 14:01 UTC](https://rubytalk.org/t/calling-irb-from-inside-an-application/43281/4 "2007-12-25T14:01:54Z")

</div>

Merry christmas!

This was mercilessly stolen and adapted from a post on Errs blog (only google has the answer ;):

require 'irb'

module IRB  
&nbsp;&nbsp;&nbsp;def self.start\_session(binding)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IRB.setup(nil)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;workspace = WorkSpace.new(binding)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @CONF[:SCRIPT]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;irb = Irb.new(workspace, @CONF[:SCRIPT])  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;irb = Irb.new(workspace)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@CONF[:IRB\_RC].call(irb.context) if @CONF[:IRB\_RC]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@CONF[:MAIN\_CONTEXT] = irb.context

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;trap("SIGINT") do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;irb.signal\_handle  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;catch(:IRB\_EXIT) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;irb.eval\_input  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end

def meths(o); puts (o.methods - Class.new.methods).join("\n"); end

def dROP! b  
&nbsp;&nbsp;old\_args = ARGV  
&nbsp;&nbsp;ARGV.size.times { ARGV.shift }

&nbsp;&nbsp;if defined? IRBHelper  
&nbsp;&nbsp;&nbsp;&nbsp;foo = Class.new  
&nbsp;&nbsp;&nbsp;&nbsp;foo.instance\_eval do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include IRBHelper  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Helper Methods: #{(foo.new.methods - Class.new.methods).sort.join(', ')}"  
&nbsp;&nbsp;&nbsp;&nbsp;include IRBHelper  
&nbsp;&nbsp;end

&nbsp;&nbsp;IRB.start\_session b  
&nbsp;&nbsp;old\_args.each { |a| ARGV \<\< a }  
end

> **···**
>
> On 25 Dec 2007, at 13:26, Stefano Crocco wrote:
> 
> > Alle martedì 25 dicembre 2007, Bertram Scharpf ha scritto:
> > 
> > > Hi,
> > > 
> > > this may be easy to anwer but it is difficult to google for.
> > > 
> > > While developping I would like to play around with objects from  
> > > time to time. Just like this:
> > > 
> > > &nbsp;&nbsp;&nbsp;irb(main):001:0\> class C ; def f ; "F" ; end ; end  
> > > &nbsp;&nbsp;&nbsp;=\> nil  
> > > &nbsp;&nbsp;&nbsp;irb(main):002:0\> irb C.new  
> > > &nbsp;&nbsp;&nbsp;irb#1(#\<C:0xb7910d40\>):001:0\> f  
> > > &nbsp;&nbsp;&nbsp;=\> "F"  
> > > &nbsp;&nbsp;&nbsp;irb#1(#\<C:0xb7910d40\>):002:0\>
> > > 
> > > In common my classes are not so easy to type in. So, I would like  
> > > to call Irb from somewhere inside the application as I do here:
> > > 
> > > &nbsp;&nbsp;&nbsp;----\>-call\_irb.rb---------------  
> > > &nbsp;&nbsp;&nbsp;#!/usr/bin/ruby
> > > 
> > > &nbsp;&nbsp;&nbsp;class C  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def f  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"F"  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > > &nbsp;&nbsp;&nbsp;end
> > > 
> > > &nbsp;&nbsp;&nbsp;if $0 == \_\_FILE\_\_ then  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;require "irb"  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$c = C.new  
> > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IRB.start  
> > > &nbsp;&nbsp;&nbsp;end  
> > > &nbsp;&nbsp;&nbsp;----\<---------------------------
> > > 
> > > and then
> > > 
> > > &nbsp;&nbsp;&nbsp;user@host $ ./call\_irb.rb  
> > > &nbsp;&nbsp;&nbsp;irb(main):001:0\> irb $c  
> > > &nbsp;&nbsp;&nbsp;irb#1(#\<C:0xb7be4ee0\>):001:0\> f  
> > > &nbsp;&nbsp;&nbsp;=\> "F"  
> > > &nbsp;&nbsp;&nbsp;irb#1(#\<C:0xb7be4ee0\>):002:0\>
> > > 
> > > Is there a way how I can call C.new's Irb directly without doing  
> > > the long-winded definition of a global variable first? I already  
> > > examined the irb sources but this seems to be well-hidden to me.
> > > 
> > > Thanks in advance and Merry Christmas,
> > > 
> > > Bertram
> > 
> > Not a direct answer to your question, but can't you load the file with the  
> > definition of the class in irb?
> > 
> > Stefano

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [25 December 2007 23:54 UTC](https://rubytalk.org/t/calling-irb-from-inside-an-application/43281/5 "2007-12-25T23:54:37Z")

</div>

Hi,

> **···**
>
> Am Mittwoch, 26. Dez 2007, 05:16:03 +0900 schrieb Ryan Davis:
> 
> > On Dec 25, 2007, at 05:08 , Bertram Scharpf wrote:
> > 
> > > Is there a way how I can call C.new's Irb directly without doing  
> > > the long-winded definition of a global variable first? I already  
> > > examined the irb sources but this seems to be well-hidden to me.
> > 
> > I'm confused and havet o assume the question is a bit vague. How is "$c = "  
> > long winded? What do you actually want to know?
> 
> I wanted to know how to do it without having to type in the same  
> constant or global variable name every time.
> 
> Bertram
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)

---

<div class="post-metadata">

**Author:** ![Bertram\_Scharpf](https://avatars.discourse-cdn.com/v4/letter/b/43a26b/32.png) [@Bertram\_Scharpf](https://rubytalk.org/u/Bertram_Scharpf)\
**Post date:** [25 December 2007 23:48 UTC](https://rubytalk.org/t/calling-irb-from-inside-an-application/43281/6 "2007-12-25T23:48:12Z")

</div>

Hi James,

> **···**
>
> Am Dienstag, 25. Dez 2007, 23:01:54 +0900 schrieb James Tucker:
> 
> > This was mercilessly stolen and adapted from a post on Errs blog (only  
> > google has the answer ;):
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[...]  
> > &nbsp;&nbsp;old\_args = ARGV  
> > &nbsp;&nbsp;ARGV.size.times { ARGV.shift }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[...]  
> > &nbsp;&nbsp;old\_args.each { |a| ARGV \<\< a }  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[...]
> 
> The code is not actually mature. The clearing could be done by an  
> ARGV.clear; this part won't work either because old\_args will be  
> cleared as well.
> 
> Besides that the solution works perfectly. Thank you very much!
> 
> Bertram
> 
> --  
> Bertram Scharpf  
> Stuttgart, Deutschland/Germany  
> [http://www.bertram-scharpf.de](http://www.bertram-scharpf.de)
