From: Brian Candler Date: 2009-07-22T16:46:19+09:00 Subject: Re: Byte–stream parsing in Ruby Elliott Cable wrote: > Hrm, #valid_encoding? is very helpful. But how can I stuff numerical > (`Fixnum`) bytes onto the string? If you're doing STDIN.read(1), you get a String. Just use << to concatenate, or the 2-argument form of read() where you supply a buffer to append to. If you are forced to use Fixnum, then try Integer#chr: >> str = "" => "" >> str << 255.chr => "\xFF" Warning: you have to deal with all the (undocumented) ruby-1.9 encoding stupidity. Have fun guessing the behaviour of each of the methods. e.g. >> str = "hello" => "hello" >> str.encoding => # >> str << 255.chr => "hello\xFF" >> str.encoding => # Surprised that the encoding changed? This means that: >> str.valid_encoding? => true until you do: >> str.force_encoding("UTF-8") => "hello\xFF" >> str.valid_encoding? => false Now have a guess what happens if you try to append another byte. Go on. >> str.encoding => # >> str << 250.chr Encoding::CompatibilityError: incompatible character encodings: UTF-8 and ASCII-8BIT from (irb):24 from /usr/local/bin/irb19:12:in `
' >> Haha, fooled you. You thought it was safe to append a non-UTF8 character to a UTF8 string (after all, you did before quite happily), but this time you get an exception. So now you have to do: >> str.force_encoding("ASCII-8BIT") => "hello\xFF" >> str << 250.chr => "hello\xFF\xFA" >> str.force_encoding("UTF-8") => "hello\xFF\xFA" >> str.valid_encoding? => false This is why I hate ruby 1.9. Regards, Brian. P.S. The above example was with ruby 1.9.2 r23158 under Linux with UTF8 locale. Behaviour may or may not be different with other 1.9.x versions and/or under different locale settings. -- Posted via http://www.ruby-forum.com/.