Unicode / Arabic Support in Ruby / Rails

Personal Intro (For frequent visitors of this blog only)

Yes, I’m checking out Rails. Mainly to see what ASP.NET MVC is missing. Very initial impressions, A Lot!

Anyway, this post is intended to be found by new visitors who already know a little Ruby, so, no introductions for this time. Maybe a different post.


Just to set expectations, there are only 2 lines needed for getting it to work. This post is just to encourage myself to keep writing…



So, let’s assume a simple website that ha a single controller “Say” with only one Action “Hello”.

The first time you try to add an Arabic word to any Ruby string, you’ll get the error:v

invalid multibyte char (US-ASCII)

syntax error, unexpected $end, expecting keyword_end

@name = "محمد"

To fix this, you need to tell Ruby that this file is written in Unicode.

This is done by adding the underlined text as the “first” line in the file:

Note that this works for all strings either you use single quotes or double quotes to construct the strings.


Interestingly, you don’t need to do this in views!

Here is how my /app/views/say/hellohtml.erb file looks like:

You realize of course the editor is not used with RTL support (which i different than the RTL of the rendered HTML, I’m here talking about the mark-up code itself). That’s fine for our example though.


Ideally, this should be enough, however, sometimes the Windows OS Console (if developing on Windows like me) gets it wrong and still causes the Arabic text when firing the server to get Arabc text omitted from response.

The solution to this is also another one line:

Here is how it looks like:


Now, this is the result:


Not much tested as I’m hardly starting this Ruby/Rails thing. If certain situations don’t work, I’ll update this post and announce the change in another one.