Ruby 1.9 allows you to specify the character encodings of I/O streams, strings, regexps, symbols, and so on. It also lets you specify the encoding of individual source files (and a complete application can be built from many files, each with different character encodings). Expect to start seeing a rash of obscure source code, at least until the initial excitement abates and cooler thinking prevails.
In the meantime, we can get away with
# encoding: utf-8
require 'mathn'
class Numeric
def ℃
(self - 32) * 5/9
end
def ℉
self * 9/5 + 32
end
end
puts 212.℃
puts 100.℉
Or, for those who'd like a peek at the start of a road that eventually leads to madness:
alias ✎ puts
✎ 212.℃
✎ 100.℉
I'm betting this post displays badly on about 50% of the machines that are used to view it. Which is reason enough to tread very lightly down this path…




s/class/module/
Works in 1.8 with -Ku as well.
Posted by: Christian Neukirchen | April 08, 2008 at 10:40 AM
*Searches Craigslist for an APL keyboard*
Posted by: Marcel Molina | April 08, 2008 at 11:17 AM
Unfortunately encodings of files also means that there's "binary mode" for non-windows machines now too, as sometimes Ruby 1.9 will decide your file is UTF-8 instead of a plain string-of-bytes now.
open filename, "rb:ascii-8bit" do |io| ... end
Will force a file's contents to be a plain string-of-bytes.
Posted by: drbrain | April 08, 2008 at 01:53 PM
Hah! Awesome. Now I just need to figure out how to make those special characters.. :(
Posted by: Jimmy Baker | April 08, 2008 at 01:53 PM
I actually use this in ruby 1.8, to build dynamically build accessor methods from a CSV file's headers, which are in german, so some of them have umlauts (äöü) or even ß. This way I can still use accessor methods, so I found this to be quite useful,
Posted by: toto | April 08, 2008 at 02:26 PM
A few months ago (September, maybe?) this was being discussed in #ruby-lang, and you got
* Kernel#√ to get a nice sqrt_2 = √ 2
* Enumerable#⊂ as an alias to Enumerable#include?
* Kernel#Σ: Σ(1, 2, 3, 4) #=> 10
* and a couple others.
You could become pretty evil if you want to ^_^
Posted by: Nicolás Sanguinetti | April 08, 2008 at 04:08 PM
I was just thinking the other day this would be a great idea. Finally dot and cross matrix multiplication can be realised nicely. This will certainly position Ruby better in the mathamatic & scientific communities.
Posted by: Chris Lloyd | April 09, 2008 at 07:50 AM
Actually, browsers do a pretty good job of displaying non-ASCII Unicode characters. In fact, the ease of transmitting non-ASCII data is about 1000 times better than it was a decade ago and character transmission problems are becoming relatively rare. Google "Japanese characters" on any modern computer. Then cut and paste it almost anywhere, even into a "vi" window in a Mac OS X terminal.
Of course there will always be exceptions of newly invented characters, or family-name characters or other odd cases. But standard symbols and accents? No problem.
Posted by: Paul Prescod | February 01, 2009 at 05:42 PM