English 中文(简体)
Unicode Strings in Ruby 1.9
原标题:

I ve written a Ruby script that is reading a file (File.read()) that contains unicode characters, and it works fine from the command line.

However, when I try to put it into an Automator Workflow (Mac OS X), I get this error;

2009-12-23 17:55:15 -0500: /Users/jeffreyaylesworth/bin/symbols:19:in `split : invalid byte sequence in US-ASCII (ArgumentError)
(traceback)

So when running from Automator, split suddenly doesn t like non ASCII characters. As far as I can tell, both are running from the same version of Ruby (the version number is the same).

I m not too concerned about why they are acting different (but if someone knows, that s great), but I would like a solution to make split accept non ASCII characters.

If it helps, I need to split text at a single character into two pieces, so if something that s similar to C s tokenizer would work, I can use that.

最佳回答

You don t specify the encoding of the file. Since it is impossible to reliably determine the encoding of a file automatically, the encoding must be explicitly specified. If it isn t, the external encoding is used, if that isn t set, the encoding specified in the environment is going to be used, and if the environment doesn t specify an encoding, the file is assumed to be in 7 bit US-ASCII.

In your case, it seems that there is either a difference in the two environments (automated scripts are often run in a very restrictive environment without locale settings) or in the way the interpreter gets invoked.

So, you d need to do something like

File.read( /path/to/file , encoding:  UTF-8 )
问题回答

Sounds like the two are being run from different environments - with different LOCALE values.





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签