Forums | Admin

Discussion Forums: help

Start New Thread Start New Thread
Message: 119181
BY: xiao li (expxiaoli)
DATE: 2012-12-17 13:25
SUBJECT: throw exception if instance is rewrite

 

#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?


Thread View

Thread Author Date
throw exception if instance is rewritexiao li2012-12-17 13:25
      RE: throw exception if instance is rewritexiao li2012-12-17 13:33
            RE: throw exception if instance is rewritearton Tajima2012-12-17 14:42
                  RE: throw exception if instance is rewritexiao li2012-12-18 07:41

Post a followup to this message