BSF scripting - setting "$_" from Java?

I have a number of useful ‘.rb’ script for processing text files (in an
old fashioned ‘awk’ sort of way :wink: ) : I would like to be run these
from JRuby…without modification if possible - having trouble setting
up the ‘$_’ variable?


BSFManager manager = new BSFManager();

String inputstring=new String(“hello there!”);
manager.declareBean("", inputstring, String.class);
manager.exec(“ruby”, “java”, 1, 1, "puts $
");

The ‘puts’ returns ‘nil’ - if I substitute ‘’ with ‘X’ and '$’ with
‘$X’ - this works just fine.

Essentially if I can get ‘$_’ setup, then I won’t have to modify my Ruby
programs…

Anybody know if there’s a way of doing this - or do I have to put up
with a different variable name here? (Similarly I often use ruby with
the ‘-an’ option - so that ‘$F[n]…’ is set up - I’m guessing this is
less of a problem, since ‘$F’ doesn’t seem to be a ‘real’ system global
?)

Cheers

John

duh…I posted without thinking about this too much $_ is a result
variable of course - the last ‘readline’ or gets…

So most of this is just misguided…

However I guess I still need a way of running the equivalent of:

ruby -an <myscript.rb> …

thanks - and sorry for the total rubbish I posted earlier :slight_smile:

I have a number of useful ‘.rb’ script for processing text files (in an
old fashioned ‘awk’ sort of way :wink: ) : I would like to be run these
from JRuby…without modification if possible - having trouble setting
up the ‘$_’ variable?


BSFManager manager = new BSFManager();

String inputstring=new String(“hello there!”);
manager.declareBean("", inputstring, String.class);
manager.exec(“ruby”, “java”, 1, 1, "puts $
");

The ‘puts’ returns ‘nil’ - if I substitute ‘’ with ‘X’ and '$’ with
‘$X’ - this works just fine.

Essentially if I can get ‘$_’ setup, then I won’t have to modify my Ruby
programs…

Anybody know if there’s a way of doing this - or do I have to put up
with a different variable name here? (Similarly I often use ruby with
the ‘-an’ option - so that ‘$F[n]…’ is set up - I’m guessing this is
less of a problem, since ‘$F’ doesn’t seem to be a ‘real’ system global
?)

Cheers

John