Strings Differ between Ruby and MacRuby?
I have a bit of an issue with Strings between MacRuby and Ruby. I have a socket that is feeding me data and I am running a check on watching for changes in the byte structure. The script works in ruby and I can get both the "\030" and the "\220" values to test true Here is the snippet. if results == "\030" puts "got $2" # @nc.postNotificationName("coins_taken_complete", object:self) elsif results == "\220" puts "got $1" # @nc.postNotificationName("coins_taken_complete", object:self) end When I get it into the MacRuby environment the "\030" is picked up by the code but the "\220" is not picked up at all. I have also tried this with the hex values "\x18" and "\x90" and I get the same result, the "\x18" is picked up and the "\x90" is not. I can see in the console that "\x90" is the value being passed to this test when building with xcode and as I mentioned when I build the script in ruby with textmate everything functions normally. So I am wondering two things: Is this a bug? If I have to do a work around is there any way to tell what encodings are being seen by the Macruby loop so I can match them Any help would be greatly appreciated Thanks, Shaun
On 2010-10-11, at 16:07 , Shaun August wrote:
The script works in ruby and I can get both the "\030" and the "\220" values to test true
When in MRI, are you testing in ruby 1.9? This reeks of encoding issues.
If I have to do a work around is there any way to tell what encodings are being seen by the Macruby loop so I can match them
I assume you know of String#encoding and __ENCODING__?
Is this a bug? Kind of. It looks like MacRuby differs from Ruby 1.9(and 1.8) in that it does not return ASCII-8BIT strings(or "byte" strings) from IO objects. Encoding.default_external is set to UTF-8, where as in Ruby 1.9 it is set to ASCII-8BIT. Encoding.default_internal is set to nil, which means data sent to and received from an IO stream will leave and be received in a UTF-8 encoded string.
If I have to do a work around is there any way to tell what encodings are being seen by the Macruby loop so I can match them
Sure, some things you can try: Encoding.default_external= Encoding.default_internal= (I explain below…) IO#set_encoding TCPSocket.new(…).set_encoding('external:internal') 'external' is the external encoding you want to interpret the IO stream in. 'internal' is the encoding you want data received from the string to be transcoded to. When you are writing to the socket, 'internal' must be transcoded to 'external'. When you are reading from the socket, 'external' must be transcoded to 'internal'. If 'internal' is nil, no transcoding takes place (unless the string is encoded in an encoding that does not match the external encoding). String#encode If those options fail, you can fall back to String#encode. a = TCPSocket.new(…) a.write("stuff\r\n") results = a.read.encode('ASCII-8BIT') I hope this helps you, Rob.
Hi Guys, Thanks for your help. I am still running 1.8.6 on my machine so I guess it is time to upgrade so I don't chase my tail for too long. I am not familiar with the character encodings but I'm reading up on them now. Thanks for your help. Shaun
participants (3)
-
Caio Chassot
-
Rob Gleeson
-
Shaun August