Skip to content

Instantly share code, notes, and snippets.

@qmx
qmx / bug_test.rb
Created November 2, 2010 01:06 — forked from griff/bug_test.rb
class BugTest1 < Hash
# Any interface will do
include Java::JavaLang::Runnable
def run
puts "Hello from BugTest1"
end
end
b
✈ (master) qmx@gondor:~/git/opensource/jruby-sandbox/430032$ java -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y -jar ../../jruby/lib/jruby.jar bug_test.rb
Listening for transport dt_socket at address: 8787
Press enter to begin
file:/Volumes/v2/git/opensource/jruby/lib/jruby.jar!/builtin/javasupport/java.rb:50:in `method_missing': Java package `real.data.new' does not have a
require 'soap/wsdlDriver'
service = SOAP::WSDLDriverFactory.new('http://localhost:8080/Dicionario?wsdl').create_rpc_driver
resposta = service.traduz(:palavra => 'bola')
puts resposta.traducao
(in /Volumes/v2/git/opensource/jruby)
....
Java instance method names
- should present as both camel-case and ruby-case
- should present javabean properties as attribute readers and writers
- should treat consecutive caps as part of one property name
- should present boolean javabean property accessors as '?' method (PENDING: not implemented)
- should not overwrite critical core Ruby methods
@qmx
qmx / 1.txt
Created November 30, 2010 12:41
Compiled from "TestVarargs.java"
public class xpto.TestVarargs extends java.lang.Object{
public xpto.TestVarargs();
Code:
0: aload_0
1: invokespecial #8; //Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:

» JRuby Project Wiki Home Page

Embedding JRuby

Using Java from Ruby is JRuby's best-known feature---but you can also go in the other direction, and use Ruby from Java. There are several different ways to do this. You can execute entire Ruby scripts, call individual Ruby methods, or even implement a Java interface in Ruby (thus allowing you to treat Ruby objects like Java ones). We refer to all these techniques generically as "embedding." This section will explain how to embed JRuby in your Java project.

Table of Contents

Red Bridge (JRuby Embed)

JRuby has long had a private embedding API, which was closely tied to the runtime's internals and therefore changed frequently as JRuby evolved. Since version 1.4, however, we have also provided a more stable public API, known as Red Bridge or JRuby Embed. Existing Java programs written to the legacy API should still work, but we strongly recommend Red Bridge for all new projects.

<p><a href='/projects/jruby/pages/Home' class='internal'>&raquo; JRuby Project Wiki Home Page</a><h1>Embedding JRuby</h1>
Using Java from Ruby is JRuby's best-known feature---but you can also go in the other direction, and use Ruby from Java. There are several different ways to do this. You can execute entire Ruby scripts, call individual Ruby methods, or even implement a Java interface in Ruby (thus allowing you to treat Ruby objects like Java ones). We refer to all these techniques generically as &quot;embedding.&quot; This section will explain how to embed JRuby in your Java project.
</p><div id='toc' class='toc'>
<div id='toctitle' class='toc-title'>
<span>Contents</span>
</div>
<div id='toccontents' class='toc-contents'><ul><li>1 <a href='#Red_Bridge_(JRuby_Embed)'> Red Bridge (JRuby Embed) </a></li>
<ul><li>1.1 <a href='#Features_of_Red_Bridge'> Features of Red Bridge </a></li>
<ul><li>1.1.1 <a href='#Context_Type'> Context Type </a></li>
package java_integration.fixtures;
import java.util.concurrent.Callable;
public class InterfaceWrapper {
public static Callable giveMeBack2(Callable call) {
return call;
}
public static Runnable giveMeBack(Runnable runnable) {
return runnable;
}
}
Calendar c = Calendar.getInstance();
c.set(2010, 0, 1, 12, 0, 0);
long timestamp = c.getTimeInMillis();
Timestamp seuTimestamp = new Timestamp(timestamp);
protected Class<T> defineClass(ClassGenerator classGenerator) {
// attempt to load from classloaders
String className = classGenerator.name();
Class contents = null;
try {
contents = getClassLoader().loadClass(className);
if (RubyInstanceConfig.JIT_LOADING_DEBUG) {
System.err.println("found jitted code in classloader: " + className);
}
} catch (ClassNotFoundException cnfe) {