# Irb: Is it possible to automatically create a subsession on startup?

**URL:** <https://rubytalk.org/t/irb-is-it-possible-to-automatically-create-a-subsession-on-startup/49885>\
**Category:** ruby-talk\
**Created:** [28 October 2008 17:19 UTC](https://rubytalk.org/t/irb-is-it-possible-to-automatically-create-a-subsession-on-startup/49885 "2008-10-28T17:19:37Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wfisk](https://avatars.discourse-cdn.com/v4/letter/w/a88e57/32.png) [@wfisk](https://rubytalk.org/u/wfisk)\
**Post date:** [28 October 2008 17:19 UTC](https://rubytalk.org/t/irb-is-it-possible-to-automatically-create-a-subsession-on-startup/49885/1 "2008-10-28T17:19:37Z")

</div>

In irb I can create an object and then create a new subsession with  
that object as the context.  
For example in irb I can type  
&nbsp;&nbsp;$psauto = PSAuto::Automator.new  
&nbsp;&nbsp;irb $psauto

Now I can call all the methids of $psauto without having to type the  
'$psauto' each time.

Is it possible to put these two lines into an irb config file so that  
I do not have to type them every time I start irb?

---

<div class="post-metadata">

**Author:** ![Roger\_Pack4](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Roger\_Pack4](https://rubytalk.org/u/Roger_Pack4)\
**Post date:** [28 October 2008 18:22 UTC](https://rubytalk.org/t/irb-is-it-possible-to-automatically-create-a-subsession-on-startup/49885/2 "2008-10-28T18:22:24Z")

</div>

> Is it possible to put these two lines into an irb config file so that  
> I do not have to type them every time I start irb?

I believe there's a ~/.irbrc  
-=R

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![William\_Fisk](https://avatars.discourse-cdn.com/v4/letter/w/ec9cab/32.png) [@William\_Fisk](https://rubytalk.org/u/William_Fisk)\
**Post date:** [28 October 2008 19:35 UTC](https://rubytalk.org/t/irb-is-it-possible-to-automatically-create-a-subsession-on-startup/49885/3 "2008-10-28T19:35:34Z")

</div>

yes I am using .irbrc, but it doesn't work when you pur  
'irb $psauto' in it. At least it doesn't work for me

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [28 October 2008 20:03 UTC](https://rubytalk.org/t/irb-is-it-possible-to-automatically-create-a-subsession-on-startup/49885/4 "2008-10-28T20:03:58Z")

</div>

William Fisk wrote:

> yes I am using .irbrc, but it doesn't work when you pur  
> 'irb $psauto' in it. At least it doesn't work for me

Try this in .irbrc:

module IRB  
&nbsp;&nbsp;&nbsp;def IRB.start\_session(\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unless $irb  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IRB.setup nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;workspace = WorkSpace.new(\*args)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @CONF[:SCRIPT] ## normally, set by parse\_opts  
&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 'INT' do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$irb.signal\_handle  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;custom\_configuration if defined?(IRB.custom\_configuration)

&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

x = Object.new  
puts "\nStarted irb shell for x"  
IRB.start\_session(x)

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![William\_Fisk](https://avatars.discourse-cdn.com/v4/letter/w/ec9cab/32.png) [@William\_Fisk](https://rubytalk.org/u/William_Fisk)\
**Post date:** [28 October 2008 20:48 UTC](https://rubytalk.org/t/irb-is-it-possible-to-automatically-create-a-subsession-on-startup/49885/5 "2008-10-28T20:48:43Z")

</div>

Joel,

Thanks for that. Unfortunately it doesn't work for me.  
I get lots of errors like the following

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152:  
warning: already initialized constant UnrecognizedSwitch  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152:  
warning: already initialized constant NotImplementedError  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152:  
warning: already initialized constant CantReturnToNormalMode  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152:  
warning: already initialized constant IllegalParameter  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152:  
warning: already initialized constant IrbAlreadyDead  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152:  
warning: already initialized constant IrbSwitchedToCurrentThread  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152:  
warning: already initialized constant NoSuchJob  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152:  
warning: already initialized constant CantShiftToMultiIrbMode  
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152:  
warning: already initialized constant CantChangeBinding

However, I googled "IRB.start\_session" and found some other interesting  
post and replies (all by you I think!); and I can see other things are  
possible.  
I'm going to look at this again tomorrow.

Thanks

William

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [28 October 2008 20:55 UTC](https://rubytalk.org/t/irb-is-it-possible-to-automatically-create-a-subsession-on-startup/49885/6 "2008-10-28T20:55:13Z")

</div>

William Fisk wrote:

> Joel,
> 
> Thanks for that. Unfortunately it doesn't work for me.  
> I get lots of errors like the following
> 
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152: warning: already initialized constant UnrecognizedSwitch  
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152: warning: already initialized constant NotImplementedError  
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152: warning: already initialized constant CantReturnToNormalMode  
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152: warning: already initialized constant IllegalParameter  
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152: warning: already initialized constant IrbAlreadyDead  
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152: warning: already initialized constant IrbSwitchedToCurrentThread  
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152: warning: already initialized constant NoSuchJob  
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152: warning: already initialized constant CantShiftToMultiIrbMode  
> /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/e2mmap.rb:152: warning: already initialized constant CantChangeBinding
> 
> However, I googled "IRB.start\_session" and found some other interesting post and replies (all by you I think!); and I can see other things are possible.  
> I'm going to look at this again tomorrow.
> 
> Thanks
> 
> William

Sorry, cut-and-paste error ☹

Removing the

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IRB.setup nil

call should help. Here's the code for .irbrc:

module IRB  
&nbsp;&nbsp;&nbsp;def IRB.start\_session(\*args)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;workspace = WorkSpace.new(\*args)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @CONF[:SCRIPT] ## normally, set by parse\_opts  
&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 'INT' do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$irb.signal\_handle  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;custom\_configuration if defined?(IRB.custom\_configuration)

&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

x = Object.new  
puts "\nStarted irb shell for x"  
IRB.start\_session(x)

> **···**
>
> --  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

---

<div class="post-metadata">

**Author:** ![William\_Fisk](https://avatars.discourse-cdn.com/v4/letter/w/ec9cab/32.png) [@William\_Fisk](https://rubytalk.org/u/William_Fisk)\
**Post date:** [28 October 2008 21:13 UTC](https://rubytalk.org/t/irb-is-it-possible-to-automatically-create-a-subsession-on-startup/49885/7 "2008-10-28T21:13:46Z")

</div>

Hey Joel,

Thanks man, that works. I did start looking at the IRB code but it was  
too complicated for me.

Now time for bed.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
