Forum: Ruby irb: Is it possible to automatically create a subsession on startup?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Df4ca550a3cbb6379587e65873b9c093?d=identicon&s=25 wfisk (Guest)
on 2008-10-28 18:20
(Received via mailing list)
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
  $psauto = PSAuto::Automator.new
  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?
Bec38d63650c8912b6ba9b557fb953b9?d=identicon&s=25 Roger Pack (rogerdpack)
on 2008-10-28 19:22
> 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
02c92bec9a0ca22e7f9527a315cf4979?d=identicon&s=25 William Fisk (wfisk)
on 2008-10-28 20:36
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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2008-10-28 21:04
(Received via mailing list)
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
   def IRB.start_session(*args)
     unless $irb
       IRB.setup nil
     end

     workspace = WorkSpace.new(*args)

     if @CONF[:SCRIPT] ## normally, set by parse_opts
       $irb = Irb.new(workspace, @CONF[:SCRIPT])
     else
       $irb = Irb.new(workspace)
     end

     @CONF[:IRB_RC].call($irb.context) if @CONF[:IRB_RC]
     @CONF[:MAIN_CONTEXT] = $irb.context

     trap 'INT' do
       $irb.signal_handle
     end

     custom_configuration if defined?(IRB.custom_configuration)

     catch :IRB_EXIT do
       $irb.eval_input
     end
   end
end

x = Object.new
puts "\nStarted irb shell for x"
IRB.start_session(x)
02c92bec9a0ca22e7f9527a315cf4979?d=identicon&s=25 William Fisk (wfisk)
on 2008-10-28 21:49
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
47b1910084592eb77a032bc7d8d1a84e?d=identicon&s=25 Joel VanderWerf (Guest)
on 2008-10-28 21:56
(Received via mailing list)
William Fisk wrote:
> 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 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

       IRB.setup nil

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

module IRB
   def IRB.start_session(*args)
     workspace = WorkSpace.new(*args)

     if @CONF[:SCRIPT] ## normally, set by parse_opts
       $irb = Irb.new(workspace, @CONF[:SCRIPT])
     else
       $irb = Irb.new(workspace)
     end

     @CONF[:IRB_RC].call($irb.context) if @CONF[:IRB_RC]
     @CONF[:MAIN_CONTEXT] = $irb.context

     trap 'INT' do
       $irb.signal_handle
     end

     custom_configuration if defined?(IRB.custom_configuration)

     catch :IRB_EXIT do
       $irb.eval_input
     end
   end
end

x = Object.new
puts "\nStarted irb shell for x"
IRB.start_session(x)
02c92bec9a0ca22e7f9527a315cf4979?d=identicon&s=25 William Fisk (wfisk)
on 2008-10-28 22:14
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.
This topic is locked and can not be replied to.