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.

Share With Friends:

P.S. Please help me out by checking this offer, then look below for a small Thank You.

How did I learn that?

As a bonus for coming here, I'm giving away a free newsletter for web developers that you can sign up for from here.
It's not an anything-and-everything link list. It's thoughtfully collected picks of articles and tools, that focus on Angular 2, ASP.NET 5, and other fullstack developer goodies.