我想將Java類傳遞給JRuby方法,並在方法中實例化類對象(我想要一種在一組Java類上運行某些測試的通用方法,需要實例化一個數量的這些對象,直到運行時才知道)的:將Java類作爲參數傳遞給JRuby方法
#...
somethingMethod(Bar)
#....
def somethingMethod(javaClass)
number.each do |n|
fu=javaClass.new
#...otherStuff
end
end
但這似乎並沒有被這種方式是可行的。我得到:
Failure/Error: somethingMethod(Bar)
NameError:
uninitialized constant Bar
# somethingTest.rb:45:in `(root)'
我也試過使用完全合格的類名:相同的結果。 謝謝。
問題是我使用com.foo.Bar而不是com :: foo :: Bar來完全限定類名(!)。和'javaClass.java_class.new'確實沒有**工作:'失敗/錯誤:somethingMethod(com :: foo :: Bar) NoMethodError: 未定義的方法new for com.foo.Bar::JavaClass' – ggg 2012-01-10 16:29:53
謝謝! :-) – ggg 2012-01-10 16:35:21