Start IRB from script

Dear All,

Can I start swing IRB from JSR 223 Script? I add jruby-complete to the
class path and try to start IRB. It does not show a command prompt and
when I press tab, the following exception occurs:

Exception in thread “AWT-EventQueue-0”
file:/d:/jruby-complete-1.5.1.jar!/META-INF/jruby.home/lib/ruby/1.8/irb/completion.rb:38:
undefined method workspace' for nil:NilClass (NoMethodError) from <script>:1:incall’
from :1
…internal jruby stack elided…
from (unknown).(unknown)(:1)
from Proc.call(:1)
from (unknown).(unknown)(:1)

The code is

ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName(“jruby”);

String t = “load ‘META-INF/jruby.home/bin/jirb_swing’”;
engine.eval(t);

I also try JRuby ScriptingContainer and BSF but the results are the
same.

Thanks,

MK

Hi,

On Tue, Jul 20, 2010 at 8:39 AM, Makaru K. [email protected]
wrote:

    from <script>:1

I also try JRuby ScriptingContainer and BSF but the results are the same.

I couldn’t run jirb_swing from JSR223, but do from Embed Core
(ScriptingContainer). I wrote the code below:

package evergreen;

import java.util.Arrays;

import org.jruby.embed.LocalContextScope;
import org.jruby.embed.ScriptingContainer;

public class JirbSwingFromClasspath {
private JirbSwingFromClasspath() {
ScriptingContainer container = new
ScriptingContainer(LocalContextScope.SINGLETHREAD);
String jrubyhome = container.getHomeDirectory();
String[] paths = {jrubyhome + “/bin”};
container.setLoadPaths(Arrays.asList(paths)); // add
“bin” directory to $LOAD_PATH
String jirb_swing = jrubyhome + “/bin/jirb_swing”;
container.setScriptFilename(jirb_swing); //
equivalent to “-S /path/to/jirb_swing” option
container.runScriptlet(“load ‘jirb_swing’”);
}

public static void main(String[] args) {
    new JirbSwingFromClasspath();
}

}

and executed this by:

java -cp /Users/yoko/Projects/jruby/lib/jruby-complete.jar:.

evergreen.JirbSwingFromClasspath

Then IRB started on a swing window.

Mainly I have two reasons I could not make this on JSR223. The first
one is getting JRuby home directory using API. ScriptingContainer has
getHomeDirectory() method, which returned
“file:/Users/yoko/Projects/jruby/lib/jruby-complete.jar!/META-INF/jruby.home”
in my environment. If you know the directory of jruby-complete.jar,
you can manage this by writing a hard-coded path. Another one is
setting the script file name, container.setSctiptFilename(). You can’t
do this on current JSR223 implementation. Perhaps,
ScriptEngine.FILENAME should have been used for this purpose. But,
current impl uses FILENAME only when evaluates a given Ruby file. Do
you need to start IRB from JSR223?

-Yoko

Thanks,
MK


The New Busy think 9 to 5 is a cute idea. Combine multiple calendars with
Hotmail. Get busy.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Fri, Jul 30, 2010 at 7:02 AM, Yoko H. [email protected] wrote:

Mainly I have two reasons I could not make this on JSR223. The first
one is getting JRuby home directory using API. ScriptingContainer has
getHomeDirectory() method, which returned
“file:/Users/yoko/Projects/jruby/lib/jruby-complete.jar!/META-INF/jruby.home”
in my environment. If you know the directory of jruby-complete.jar,
you can manage this by writing a hard-coded path.

I don’t suppose there might be another way around this?

For instance, for normal IRB we can do this:

require 'irb'
IRB.start(__FILE__)

Although at present, obscure permissions issues in a secure
environment prevents this working under JRuby, it does at least get to
starting IRB before failing. :slight_smile:

jirb_swing is a lot more complicated, but if its important bits were
moved into a library which would be found when a script requires
‘jirb_swing’, then we would be able to do it elegantly:

require 'jirb_swing'
JIRB.start(__FILE__)

Another one is setting the script file name, container.setSctiptFilename(). You can’t
do this on current JSR223 implementation.

Maybe I’m misunderstanding, but we are using

//ScriptContext context
context.setAttribute(ScriptEngine.FILENAME, file,

ScriptContext.ENGINE_SCOPE);

And this appears to set FILE from within the script… but may
this other setScriptFilename() does something different, as I haven’t
been using ScriptContainer quite as much as the standard mechanism.

TX


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Thu, Jul 29, 2010 at 7:18 PM, Trejkaz [email protected] wrote:

For instance, for normal IRB we can do this:

require ‘irb’
IRB.start(FILE)

Although at present, obscure permissions issues in a secure
environment prevents this working under JRuby, it does at least get to
starting IRB before failing. :slight_smile:

Do you have any error message or exception when starting IRB failed?

Maybe I’m misunderstanding, but we are using

//ScriptContext context
context.setAttribute(ScriptEngine.FILENAME, file,
ScriptContext.ENGINE_SCOPE);

And this appears to set FILE from within the script… but may
this other setScriptFilename() does something different, as I haven’t
been using ScriptContainer quite as much as the standard mechanism.

Yes, you can set FILE when you use eval methods with Reader
object. When a script is given as a String, ScriptEngine.FILENAME
didn’t work.

OK. So, I fixed this problem in rev. 7bda00f. I also fixed classpath
setting bug in this commit. Now, you can start swing IRB from JSR223
like in below:

package evergreen;

import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class JirbSwingJsr223 {
String jrubyhome =
“file:/Users/yoko/Projects/jruby/lib/jruby-complete.jar!/META-INF/jruby.home”;

private JirbSwingJsr223() throws ScriptException {
    System.setProperty("org.jruby.embed.localcontext.scope",

“singlethread”);
System.setProperty(“org.jruby.embed.class.path”, jrubyhome +
“/bin”);
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName(“jruby”);
engine.getContext().setAttribute(ScriptEngine.FILENAME,
jrubyhome + “/bin/jirb_swing”, ScriptContext.ENGINE_SCOPE);
engine.eval(“load ‘jirb_swing’”);
}

public static void main(String[] args) throws ScriptException {
    new JirbSwingJsr223();
}

}

-Yoko

TX


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email