Skip to content

Commit e993885

Browse files
author
Pere Urbon-Bayes
committed
Initial commit with basic class and object creation for a jruby extension.
1 parent f49ef9f commit e993885

File tree

6 files changed

+131
-0
lines changed

6 files changed

+131
-0
lines changed

extensions/basic/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## JRuby Extensions basic concepts
2+
3+
This project aims to demo small and easy concepts around jruby extensions, this concepts will be listed as
4+
5+
* Classes and Modules
6+
* Static methods in both elements
7+
* Loading the extensions
8+
* ...

extensions/basic/basic.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "java"
2+
$CLASSPATH << File.join(File.dirname(__FILE__), "jruby-ext", "target", "classes")
3+
4+
require "com/purbon/basic"
5+
6+
7+
bar = Bar.new
8+
9+
puts bar.say
10+
puts bar.shout
11+
12+
class MyRubyClass
13+
include Foo
14+
end
15+
16+
puts MyRubyClass.new.build_string
17+
puts Foo.build_string

extensions/basic/jruby-ext/pom.xml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.purbon</groupId>
8+
<artifactId>jruby-ext</artifactId>
9+
<version>1.0</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>org.jruby</groupId>
14+
<artifactId>jruby</artifactId>
15+
<version>1.7.21</version>
16+
</dependency>
17+
</dependencies>
18+
19+
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.purbon;
2+
3+
import org.jruby.Ruby;
4+
import org.jruby.RubyClass;
5+
import org.jruby.RubyObject;
6+
import org.jruby.RubyString;
7+
import org.jruby.anno.JRubyClass;
8+
import org.jruby.anno.JRubyMethod;
9+
import org.jruby.runtime.ThreadContext;
10+
import org.jruby.runtime.builtin.IRubyObject;
11+
12+
/**
13+
* A basic class created for learning purpouses
14+
* Created by purbon on 24/08/15.
15+
*/
16+
17+
@JRubyClass(name = "Bar")
18+
public class Bar extends RubyObject {
19+
20+
public Bar(Ruby ruby, RubyClass metaclass) {
21+
super(ruby, metaclass);
22+
}
23+
24+
@JRubyMethod(module = true, name = {"shout", "say"})
25+
public static RubyString shout(ThreadContext context, IRubyObject self) {
26+
return context.runtime.newString("Hello World!");
27+
}
28+
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.purbon;
2+
3+
import org.jruby.Ruby;
4+
import org.jruby.RubyClass;
5+
import org.jruby.RubyModule;
6+
import org.jruby.runtime.ObjectAllocator;
7+
import org.jruby.runtime.builtin.IRubyObject;
8+
import org.jruby.runtime.load.BasicLibraryService;
9+
10+
import java.io.IOException;
11+
12+
/**
13+
* This class is used when to run the instanciation and load
14+
* of all the related modules and classes defined here.
15+
* Created by purbon on 24/08/15.
16+
*/
17+
public class BasicService implements BasicLibraryService {
18+
19+
public boolean basicLoad(final Ruby ruby) throws IOException {
20+
21+
RubyModule foo = ruby.defineModule("Foo");
22+
foo.defineAnnotatedMethods(Foo.class);
23+
24+
RubyClass bar = ruby.defineClass("Bar", ruby.getObject(), new ObjectAllocator() {
25+
public IRubyObject allocate(Ruby ruby, RubyClass rubyClass) {
26+
return new Bar(ruby, rubyClass);
27+
}
28+
});
29+
bar.defineAnnotatedMethods(Bar.class);
30+
31+
return true;
32+
}
33+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.purbon;
2+
3+
import org.jruby.*;
4+
import org.jruby.anno.JRubyMethod;
5+
import org.jruby.anno.JRubyModule;
6+
import org.jruby.runtime.ThreadContext;
7+
import org.jruby.runtime.builtin.IRubyObject;
8+
9+
/**
10+
* A basic module named Foo
11+
* Created by purbon on 24/08/15.
12+
*/
13+
@JRubyModule( name = "Foo")
14+
public class Foo extends RubyObject {
15+
16+
public Foo(Ruby ruby, RubyClass metaclass) {
17+
super(ruby, metaclass);
18+
}
19+
20+
@JRubyMethod( module = true, name = { "build_string", "new_string" } )
21+
public static RubyString buildAnString(ThreadContext context, IRubyObject self) {
22+
Ruby runtime = context.runtime;
23+
return runtime.newString("This is a new String");
24+
}
25+
}

0 commit comments

Comments
 (0)