Reputation: 13378
Here's my code:
text = File.read('/Users/username/Desktop/text.txt')
replace = text.gsub('hai', 'hǎi')
File.open('/Users/username/Desktop/text.txt', "w") {|file| file.puts replace}
I am trying to run this script from my terminal, but it gives this error:
XXXX:Desktop username$ ruby replace.rb
replace.rb:2: invalid multibyte char (US-ASCII)
replace.rb:2: invalid multibyte char (US-ASCII)
replace.rb:2: syntax error, unexpected $end, expecting ')'
replace = text.gsub('hai', 'hǎi')
^
I actually want to replace lots of characters with accented characters in my CSV file which will later be exported to my MySQL database.
Upvotes: 0
Views: 980
Reputation: 342333
you can try putting -Ku
#!/usr/bin/env ruby -Ku
text = File.read('file')
replace = text.gsub('hai', 'hǎi')
File.open('file1', "w") {|file| file.puts replace}
Upvotes: 2
Reputation: 2010
Do these two links help? Unicode strings in Ruby code, Bytes and characters in Ruby 1.8
You should probably also learn about the basics of Unicode.
Upvotes: 0