From: Ryan Smith on
Here is my code:

============File Main.rb ================

# -*- coding: utf-8 -*-
$KCODE = "U"
$LOAD_PATH << File.dirname(__FILE__).to_s+"/lib"
require 'iv'
require 'misc'
require 'DBI'
require "watir"

url="http://www.baidu.com"
ie = Watir::IE.new
ie.goto url
t=ie.title
p t

t=t.toutf8()
Misc.cn2en(t)



============File misc.rb ================
module Misc
require "rtranslate"

def Misc.cn2en(longMixTxt)
#s="\344\270\255\345\233\275\344\270\255\345\233\275china,\344\270\255\345\233\275,china2."
s=longMixTxt
cnarray=[]
enarray=[]

cnarray=s.scan(/[^[:alnum:][:punct:] ]+/); p cnarray
cnarray.each{ |x| puts x; enarray << Misc.googletranslatetoen(x) }
enarray.each{ |x| i=enarray.index(x); p i.to_s; p cnarray[i] ; p x; p
s.include?(cnarray[i]); s=s.gsub(/cnarray[i]/, x.to_s) ; p s ;}

return s
end #Misc.cn2en(longMixTxt)


==========output =============
C:\ruby_project>main.rb -b
"鐧惧害涓€涓嬶紝浣犲氨鐭ラ亾"
["鐧惧害涓€涓嬶紝浣犲氨鐭ラ亾"]
鐧惧害涓€涓嬶紝浣犲氨鐭ラ亾
start translate cn->en
"0"
"鐧惧害涓€涓嬶紝浣犲氨鐭ラ亾"
"Baidu about, you know"
true
"鐧惧害涓€涓嬶紝浣犲氨鐭ラ亾"



==== THANKS EVERYBODY WHO CARE THIS POST====
--
Posted via http://www.ruby-forum.com/.

From: Ryan Smith on
I found my mistake is using incorrect reg match, it works after change
to

s=s.gsub(/#{cnarray[i]}/, x.to_s)

thanks everyone!
--
Posted via http://www.ruby-forum.com/.

First  |  Prev  | 
Pages: 1 2
Prev: Mapping error in soap4r
Next: What am I doing wrong?