Skip to content

Commit 294a3e4

Browse files
author
monkstone
committed
add module class method to RubyFoo
1 parent b77899f commit 294a3e4

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ public static IRubyObject buildString(ThreadContext context, IRubyObject recv) {
3232
Ruby runtime = context.getRuntime();
3333
return runtime.newString("This is a new String");
3434
}
35+
36+
/**
37+
* This is a kind of pointless module class method, but is simple to understand.
38+
* meta = true is waht makes this module class method
39+
* The equivalent in ruby:
40+
* module Foo
41+
* def self.bself_string
42+
* return 'This is String is from Foo.self'
43+
* end
44+
* end
45+
*
46+
* @param context ThreadContext
47+
* @param recv the receiver
48+
* @return A RubyString.
49+
*/
50+
51+
@JRubyMethod(name = "self_string", module = true, meta = true)
52+
public static IRubyObject buildSelfString(ThreadContext context, IRubyObject recv) {
53+
Ruby runtime = context.getRuntime();
54+
return runtime.newString("This is String is from Foo.self");
55+
}
3556

3657

3758
/**

extensions/basic/test/foo_test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ def test_build_string
2222
assert_equal foo.build_string, 'This is a new String', 'build_string failed'
2323
end
2424

25+
def test_self_string
26+
assert_equal Foo.self_string, 'This is String is from Foo.self', 'Failed module class method'
27+
refute_respond_to foo, :self_string, 'Failed to refute instance method'
28+
end
29+
2530
def test_alias
2631
assert_equal foo.build_string, foo.new_string, 'alias failed'
2732
end

0 commit comments

Comments
 (0)