Ruby: Source Code Encoding

By Xah Lee. Date: . Last updated: .

Ruby 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

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, beginner info