· ruby

Ruby: invalid multibyte char (US-ASCII)

I’ve used Ruby on and off for the last few years but somehow had never come across the following error which we got last week while attempting to print out a currency value:

blah.ruby

amount = "£10.00"
puts amount
$ ruby blah.ruby
blah.ruby:1: invalid multibyte char (US-ASCII)
blah.ruby:1: invalid multibyte char (US-ASCII)

Luckily my pair Jae had come across this before and showed me a blog post which explains what’s going on and how to sort it out.

It turns out that since Ruby 1.9 we need to explicitly specify which encoding we want to use for strings otherwise it will default to US-ASCII in my case at least.

We can get around this by explicitly telling the interpreter that we want to use UTF-8 encoding by including the following comment at the top of our file:

# encoding: UTF-8
amount = "£10.00"
puts amount

Then when we interpret the file this time:

$ ruby blah.ruby
£10.00

Since we were only editing one file it made sense to just set the comment manually but if we need to do that in a more widespread way then we’d want to use the magic_encoding gem.

Yehuda Katz also wrote a post a few years ago explaining all things encoding in more detail.

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket