From cbe7a511c32e0687300f1ec6394a924b7235fdf1 Mon Sep 17 00:00:00 2001 From: gogotanaka Date: Wed, 3 Dec 2014 15:17:27 -0800 Subject: [PATCH 1/2] Improve Type interface --- lib/java/type.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/java/type.rb b/lib/java/type.rb index 0040d59..8811039 100644 --- a/lib/java/type.rb +++ b/lib/java/type.rb @@ -16,9 +16,10 @@ def match?(val) class << self def define_new(sym, klass, &condition) + type = find_or_create(sym, klass, &condition) Module.class_eval do define_method(sym) do |meth| - define_typed_method(meth, Type.find_or_create(sym, klass, &condition)) + define_typed_method(meth, type) end private sym end @@ -31,5 +32,9 @@ def find(sym) def find_or_create(sym, klass, &condition) find(sym) || new(sym, klass, &condition) end + + def types + @@types + end end end From 66ee2878527683e00c7f5e8bb7fa3ffb53a41951 Mon Sep 17 00:00:00 2001 From: gogotanaka Date: Wed, 3 Dec 2014 16:29:11 -0800 Subject: [PATCH 2/2] Init JavaClass module --- lib/java/java_class.rb | 13 +++++++++++++ test/java_test.rb | 6 +++--- 2 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 lib/java/java_class.rb diff --git a/lib/java/java_class.rb b/lib/java/java_class.rb new file mode 100644 index 0000000..b6abaf0 --- /dev/null +++ b/lib/java/java_class.rb @@ -0,0 +1,13 @@ +module JavaClass + def method_missing(meth, *args, &block) + if block + eval <<-RUBY_CODE + define_method(#{meth}) do |#{args.join(',')}| + #{block.call} + end + RUBY_CODE + else + meth + end + end +end diff --git a/test/java_test.rb b/test/java_test.rb index 44519a6..a6640c1 100644 --- a/test/java_test.rb +++ b/test/java_test.rb @@ -96,10 +96,10 @@ def test_user_type private def define_test_method(type, val) - klass = Class.new.class_eval <<-RUBY_CODE - public #{type} def call + klass = Class.new.extend(JavaClass).class_eval <<-RUBY_CODE + public #{type} call(){" ObjectSpace._id2ref(#{val.__id__}) - end + "} RUBY_CODE klass.new