 |
Forums |
Admin Discussion Forums: help Start New Thread
By: arton Tajima
RE: throw exception if instance is rewrite [ reply ] 2012-12-17 14:42
|
I think you may add ! at end of the replace because it replace the instance it self.
class Jstr
JSTR = Rjb::import('java.lang.String')
def initialize(str)
@instance = JSTR.new_with_sig('...
end
def replace!(ori, other)
# replaceAll resturns Ruby string (not Rjb Proxy), so you may recreate Proxy instance
@instance = jSTR.new_with_sig('...', @instance._invoke ...)
end
def getStr
@instance.to_string
end
end
regards.
|
By: xiao li
RE: throw exception if instance is rewrite [ reply ] 2012-12-17 13:33
|
I use a ugly way to work around.
class Jstr
def initialize(str)
class_name = Rjb::import('java.lang.String')
@instance = class_name.new_with_sig('Ljava.lang.String;', str)
end
def replace(ori, other)
@instance._invoke('replaceAll', 'Ljava.lang.String;Ljava.lang.String;', ori, other) # not rewrite instance
end
def getStr
@instance.toString
end
end
jstr = Jstr.new("happy li lei")
jstr = Jstr.new(jstr.replace("lei","lei2")) #rewrite instance with another "new" method. But I think this method makes code complex and ugly.
puts jstr.getStr
|
By: xiao li
throw exception if instance is rewrite [ reply ] 2012-12-17 13:25
|
#jstr.rb
require 'rubygems'
require 'rjb'
class Jstr
def initialize(str)
class_name = Rjb::import('java.lang.String')
@instance = class_name.new_with_sig('Ljava.lang.String;', str)
end
def replace(ori, other)
@instance = @instance._invoke('replaceAll', 'Ljava.lang.String;Ljava.lang.String;', ori, other) # code A
end
def getStr
@instance.toString # code B
end
end
jstr = Jstr.new("happy li lei")
jstr.replace("lei","lei2")
puts jstr.getStr
running the above code, it throws exception: jstr.rb:15:in `getStr': undefined method `toString' for "happy li lei2":String (NoMethodError)
This exception seems due to rewrite instance at code A. How can I solve it if I wish to rewrite instance?
|
|
 |