From: Brian Candler Date: 2008-11-27T06:49:58+09:00 Subject: [ruby-core:20130] Re: Playing with String#bytes On Thu, Nov 27, 2008 at 03:03:49AM +0900, Rados��aw Bu��at wrote: > What about: > data.force_encoding("ASCII-8BIT")[1,3].bytes.to_a > ? But that changes the encoding of 'data' as a side-effect. To prevent that, you'd need data.dup.force_encoding("ASCII-8BIT")[1,3].bytes.to_a which is getting a bit messy. OTOH, I'm not sure how often you'd want to handle a string which has been tagged as UTF-8 in this way. BTW, "BINARY" is a synonym for "ASCII-8BIT" and probably makes more sense here. But oddly, you can't use a Symbol. irb(main):036:0> data.force_encoding("binary") => "\xC3\xA9\xC3\xA9" irb(main):037:0> data.encoding => # irb(main):038:0> data.force_encoding(:binary) TypeError: can't convert Symbol into String from (irb):38:in `force_encoding' from (irb):38 from /usr/local/bin/irb19:12:in `
'