Inwoking irb from the middle of things

Hello,

i know that i can inwoke irb from middle of program, the smallest
example is:

iowa@moon:~/testing$ cat inwokeirb.rb
#!/usr/bin/env ruby
require 'irb’
def main
a = 5
IRB::start $STDIN
puts "After IRB: a=#{a}"
end
main

EOF

But I can’t do anything useful from the irb session. What I want to
do is look arround int variables and probably change their values.

iowa@moon:~/testing$ ./inwokeirb.rb
irb(main):001:0> p a
NameError: undefined local variable or method `a’ for #Object:0x401b5a24
from (irb):1
irb(main):002:0> a = 2
=> 2
irb(main):003:0> quit
After IRB: a=5

End of sesion

In this time I’m living at the edge:
iowa@moon:~/testing$ ruby -v
ruby 1.7.3 (2002-11-27) [i686-linux]

The nearest goal is to fiddle with ruby interactively in some
checkpoints in the program.

The far goal is to have fully interactive environment where in time of
failure the program doesn’t end but fires interactive session.

Maybe I’m on the wrong road and have to go another direction.

···


Radek Hnilica

No matter how far down the wrong road you’ve gone, turn back.
Turkish proverb

The far goal is to have fully interactive environment where in time of
failure the program doesn't end but fires interactive session.

Well, probably you can also look at debug.rb and see if you can modify it
to do what you want.

Guy Decoux

Hi,

But I can’t do anything useful from the irb session. What I want to
do is look arround int variables and probably change their values.

Irb works in separated binding.

The nearest goal is to fiddle with ruby interactively in some
checkpoints in the program.

The far goal is to have fully interactive environment where in time of
failure the program doesn’t end but fires interactive session.

Like this?

require ‘irb’
IRB.setup(nil)
irb = IRB::Irb.new(IRB::WorkSpace.new(binding))
IRB.conf[:MAIN_CONTEXT] = irb.context
a = 10
begin
raise “#{a} left” if (a -= 1) > 0
rescue StandardError, ScriptError
puts $!.to_s
trap(“SIGINT”) {irb.signal_handle}
catch(:IRB_EXIT) {irb.eval_input} or retry
end

···

At Fri, 29 Nov 2002 21:18:28 +0900, Radek Hnilica wrote:


Nobu Nakada

Hi,

From: “Radek Hnilica” Radek@Hnilica.CZ
Sent: Friday, November 29, 2002 9:18 PM

The far goal is to have fully interactive environment where in time of
failure the program doesn’t end but fires interactive session.

$ cat catch.rb
require ‘irb’

module IRB
def IRB.start2(eval_binding, ap_path)
IRB.setup(ap_path)
irb = Irb.new(WorkSpace.new(eval_binding))
@CONF[:MAIN_CONTEXT] = irb.context
trap(“SIGINT”) do
irb.signal_handle
end
catch(:IRB_EXIT) do
irb.eval_input
end
end
end

set_trace_func proc { |event, file, line, id, binding, *rest|
if event == ‘raise’
$stdout.printf “%s:%d: `%s’ (%s)\n”, file, line, $!, $!.class
tb = caller(0)[2…-1]
for i in tb
$stdout.printf “\tfrom %s\n”, i
end
IRB.start2(binding, $stdin)
end
}

$ ruby17 -rcatch -e ‘a = 5; raise rescue RuntimeError; p a’

You can catch an exception and inspect a binding where
an error occurred. But… after quitting IRB session,
your program will exit silently. e.g. “p a” above won’t
executed.

Hmm. Doing catch/throw when “raise” before “rescue” might
let ruby confuse though I don’t know anything about it.

Back to the topic, as ts stated, debug.rb could help you.

$ ruby -rdebug -e ‘a = 5; raise rescue RuntimeError; p a’
Debug.rb
Emacs support available.

-e:1:a = 5; raise rescue RuntimeError; p a
(rdb:1) cont
-e:1: `’ (RuntimeError)
from -e:1
-e:1:a = 5; raise rescue RuntimeError; p a
(rdb:1) a = 6
6
(rdb:1) cont
6

Bear in mind you cannot recover(resume) the exception to
continue execution. We don’t have standard way to do it.

Regards,
// NaHi

I got a help from NaHi

iowa@moon:~/testing$ vi inwokeirb2.rb ; cat inwokeirb2.rb
#!/usr/bin/env ruby
require ‘irb’
module IRB
def IRB.start2(bind, ap_path)
IRB.setup(ap_path)
irb = Irb.new(WorkSpace.new(bind))
@CONF[:MAIN_CONTEXT] = irb.context
trap(“SIGINT”) do
irb.signal_handle
end
catch(:IRB_EXIT) do
irb.eval_input
end
end
end

def main
a = 5
#IRB::start $STDIN
IRB::start2 binding, $STDIN
puts “After IRB: a=#{a}”
end
main
iowa@moon:~/testing$ ./inwokeirb2.rb
irb(main):001:0> p a
5
=> nil
irb(main):002:0> a = 2
=> 2
irb(main):003:0> quit
After IRB: a=2

···

In article 200211291258.gATCwKK10547@moulon.inra.fr, ts wrote:

The far goal is to have fully interactive environment where in time of
failure the program doesn’t end but fires interactive session.

Well, probably you can also look at debug.rb and see if you can modify it
to do what you want.


Radek Hnilica

No matter how far down the wrong road you’ve gone, turn back.
Turkish proverb