Ruby: Source Code Encoding and Default Encoding for String

By Xah Lee. Date: . Last updated: .

Ruby's Default Source Code Encoding

Ruby's default source code encoding is. UTF-8.

This means, you should set your text editor to save ruby file in UTF-8. [see Set Text Editor File Encoding]

# ruby

p "abc♥".encoding.name == "UTF-8"

Declare a Encoding for Ruby Source Code

Ruby allows you to use different encoding for reading ruby source code file.

put this line as the first line of the file:

# -*- coding: utf-8 -*-

This makes UTF-8 as the source code's encoding.

This convention came from emacs. [see Emacs: Declare File Encoding]

Warning: by declaring a encoding, it also makes strings in the code in that encoding.

Ruby: Char Encoding

Ruby String