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

W

wfisk

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?
 
R

Roger Pack

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
 
W

William Fisk

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
 
J

Joel VanderWerf

William said:
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)
 
W

William Fisk

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
 
J

Joel VanderWerf

William said:
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

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)
 
W

William Fisk

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top