package com.ociweb.jruby; import java.util.Iterator; import java.util.Map; import org.jruby.*; import org.jruby.runtime.ThreadContext; public class JRubyHelper { private IRuby ruby = Ruby.getDefaultInstance(); private ThreadContext threadContext = Ruby.getDefaultInstance().getCurrentContext(); public JRubyHelper() { setLoadPath(); } private void addLoadPath(String path) { String expr = "$: << '" + path + "'"; ruby.evalScript(expr); } public RubyObject callMethod(RubyObject receiver, String methodName) { return (RubyObject) receiver.callMethod(threadContext, methodName); } public void enableWarning() { // Gives lots of "Useless use of :: in void context" warnings // that don't make sense! ruby.setVerbose(ruby.getTrue()); } public Object evalScript(String script) { return ruby.evalScript(script); } public static Object getAttribute( RubyObject object, String attributeName) { // The keys in this RubyHash are java.lang.String objects. // Are the values always java.lang.String object? RubyHash attributes = (RubyHash) object.getInstanceVariable("@attributes"); return attributes == null ? null : attributes.get(attributeName); } public static void listInstanceVariables( String name, RubyObject object) { System.out.println("Instance variables of " + name + ":"); Map vars = object.getInstanceVariables(); Iterator iter = vars.keySet().iterator(); while (iter.hasNext()) { Object key = iter.next(); Object value = vars.get(key); System.out.println(" " + key + " = " + value); } } private void setLoadPath() { String jrubyLib = System.getProperty("jruby.lib"); addLoadPath(jrubyLib); addLoadPath(jrubyLib + "/ruby/site_ruby/1.8"); addLoadPath(jrubyLib + "/ruby/site_ruby/1.8/java"); addLoadPath(jrubyLib + "/ruby/site_ruby"); addLoadPath(jrubyLib + "/ruby/1.8"); addLoadPath(jrubyLib + "/ruby/1.8/java"); addLoadPath("lib/ruby/1.8"); } }
Copyright © 2007 Object Computing, Inc. All rights reserved.