Skip to content

Define method by braces #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/java/java_class.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion lib/java/type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
6 changes: 3 additions & 3 deletions test/java_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down