forked from mvolkmann/mvolkmann.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJRubyHelper.html
More file actions
88 lines (73 loc) · 2.53 KB
/
Copy pathJRubyHelper.html
File metadata and controls
88 lines (73 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR.html1/DTD.html1-transitional.dtd">
<html xmlns="http://www.w3.org/1999.html">
<head>
<title>JRubyHelper.java</title>
<link rel="stylesheet" type="text/css" href="../common.css"/>
</head>
<body>
<h2>JRubyHelper.java</h2>
<pre><div class="code">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");
}
}</div></pre>
<hr/>
<p style="text-align:center">
Copyright © 2007 Object Computing, Inc. All rights reserved.
</p>
</body>
</html>