Skip to content

Commit d44fd20

Browse files
author
Pere Urbon-Bayes
committed
Add simple aritmetic methods to the Bar class
1 parent 1f8b8de commit d44fd20

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

extensions/basic/basic.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
puts bar.say
1717
puts bar.shout
1818

19+
puts bar.add(4, 4)
20+
1921
##
2022
# Using a module defined in the extensione.
2123
# Foo is a module defined in jruby extension point.

extensions/basic/jruby-ext/src/main/java/com/purbon/Bar.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.purbon;
22

3-
import org.jruby.Ruby;
4-
import org.jruby.RubyClass;
5-
import org.jruby.RubyObject;
6-
import org.jruby.RubyString;
3+
import org.jruby.*;
74
import org.jruby.anno.JRubyClass;
85
import org.jruby.anno.JRubyMethod;
96
import org.jruby.runtime.ThreadContext;
@@ -26,4 +23,24 @@ public static RubyString shout(ThreadContext context, IRubyObject self) {
2623
return context.runtime.newString("Hello World!");
2724
}
2825

26+
@JRubyMethod(module = true, name = "add")
27+
public IRubyObject add(ThreadContext context, IRubyObject a, IRubyObject b) {
28+
return a.callMethod(context, "+", b);
29+
}
30+
31+
@JRubyMethod(module = true, name = "sub")
32+
public IRubyObject sub(ThreadContext context, IRubyObject a, IRubyObject b) {
33+
return a.callMethod(context, "-", b);
34+
}
35+
36+
@JRubyMethod(module = true, name = "div")
37+
public IRubyObject div(ThreadContext context, IRubyObject a, IRubyObject b) {
38+
return a.callMethod(context, "/", b);
39+
}
40+
41+
@JRubyMethod(module = true, name = "plus")
42+
public IRubyObject plus(ThreadContext context, IRubyObject a, IRubyObject b) {
43+
return a.callMethod(context, "*", b);
44+
}
45+
2946
}

0 commit comments

Comments
 (0)