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

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?

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/\.

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/.

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)

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

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/.

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 :frowning:

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)

···

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

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/.