Encoding files with utf-8 using Ruby

I can’t set ruby to use the utf-8 for encoding files.

Script like this

# encoding: UTF-8
puts "ą"

works fine

but such

# encoding: UTF-8
File.open("test.txt", "w:UTF-8") do |f|
  f.write "ą"
end

causes the console pops up

task.rb: 4: invalid multibyte char (UTF-8)

despite the fact that all commands turning on utf-8 encoding are
applied.

I’m using ruby 2.0.0-p451 from rubyinstaller for windows.

It’s wor in my windows 7 box/ npp editor

Is your editor use utf-8 ?

I don’t know I’m using notepad++

Ok everything works fine, I just changed enconding in notepad++ from
ansi to utf-8.

You do not need to add #encoding: utf-8 if you are using Ruby <= 2.0 .
The files by default are treated like utf-8.