Ruby Forum JRuby > BSF scripting - setting "$_" from Java ?

Posted by John Pritchard-williams (monojohnny)
on 13.08.2008 00:49
I have a number of useful '.rb' script for processing text files (in an
old fashioned 'awk' sort of way ;-) ) : 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
Posted by John Pritchard-williams (monojohnny)
on 13.08.2008 00:58
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> <input-file> <input-file2>....

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


> I have a number of useful '.rb' script for processing text files (in an
> old fashioned 'awk' sort of way ;-) ) : 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