On 11/12/06, Miquel Oliete <ktalanet / yahoo.es> wrote:
> Hi All
>
> I have a problem (newbie problem).
>
> I don't know how to write a file using utf-8 encoding. Can you help
> me.

Well, how are you storing the Unicode characters are you using
internally? If your Unicode string within Ruby is stored as an array
of ints, then

File.open("output_file.utf8") do |fp|
  fp.puts(data.pack("U*"))
end

should be sufficient. If you have a Ruby string that uses some other
encoding (e.g. ISO-8859-1), then you must use the iconv library to
convert the string to UTF-8:

require 'iconv'

cd = Iconv.new('utf-8', 'iso-8859-1')
File.open("output_file.utf8") do |fp|
  fp.puts(cd.iconv(data))
end

When you do i18n, l10n, and m17n, strings become meaningless unless
they have an attached encoding.