On Mon, 2006-12-04 at 22:40 +0900, ciapecki wrote:
> Is there a way in ruby to:
> - open a file encoded in ucs-2le,
> - replace every occurance of '\t' (X'0009') with ',' (X'002c'),
> - and save it back in ucs-2le, without loosing any content?

Well, you _could_ do it with iconv:

$ irb -riconv

data = File.read('test')
# => "a\000b\000c\000\t\000\273\006\t\0001\000"

str = Iconv.iconv('utf-8', 'ucs-2le', data).first
# => "abc\t\332\273\t1"

newstr = str.tr("\t", ',')
# => "abc,\332\273,1"

newdata = Iconv.iconv('ucs-2le', 'utf-8', newstr).first
# => "a\000b\000c\000,\000\273\006,\0001\000"

But that strikes me as unnecessary when you could just do:

newdata = File.read('test').tr("\t", ',')
# => "a\000b\000c\000,\000\273\006,\0001\000"

;) 

Hope that helps,
-- 
Ross Bamford - rosco / roscopeco.REMOVE.co.uk