Fun with Unicode

by Daniel Berger

Did you know you can do this with Ruby out of the box?

# A real lambda
λ { puts 'Hello' }.call => 'Hello'

# Sigma - sum of all elements
∑(1,2,3) => 6

# Square root
√ 49 => 7.0

How difficult was this to implement? Keep reading!

22 Comments

Jordi Bunster
2007-10-31 20:06:34
Cool! Works equally well inside Array, to do things such as [ 1, 2, 3 ].∑
Flurin Egger
2007-11-01 02:02:21
Cool stuff :)


You could even make this shorter by changing the Sum method to args.inject(0){|sum,i| sum += i }

Christoffer Sawicki
2007-11-01 03:45:50
Flurin Egger: Why +=? There's no point in assigning to sum since it's the block's return value that is kept.
Phil
2007-11-01 09:10:47
Yay; pretty-lambdas (http://www.emacswiki.org/cgi-bin/wiki/PrettyLambda) is no longer necessary for ruby-mode. Now how do you type that again?
Daniel Berger
2007-11-01 11:44:51
@Flurin - I could have used inject, except I hate that method.


@Phil - I used MS Windows character map and did a copy/paste.

Daniel Lyons
2007-11-01 15:06:42
You should love Array#inject. And also, you should have ∑ 1,2,3, not ∑ [1,2,3] because of your asterisk.
she
2007-11-01 15:41:42
why should he love it


i myself hate several things and feel better not
using them, in the end whats important is loving to
use RUBY, not some method or specific way :)

Daniel Berger
2007-11-01 16:02:23
@Daniel - whoops, yeah, fixed. Well, I could also flatten all arguments, but ok.
Tammer Saleh
2007-11-01 16:20:07
That's great! I was honestly just thinking about this for replacing #lambda 10 minutes ago, but I wouldn't have known about the -Ku flag.


Very cool trick.

schmidt
2007-11-02 03:20:33
When you want to use this feature, but are not able to guarantee the command line switch, you may as well execute


$KCODE = "u"


before using Unicode characters in method names.

Daniel Berger
2007-11-02 04:41:16
@schmidt - I tried setting that at the top of the code, but it doesn't work. I get parse errors. I'll have to ask on the list as to why.
Gregory Brown
2007-11-03 06:04:50
Hah! Excellent.


Just the hacky kind of thing this blog needs.

Radarek
2007-11-03 12:02:43
I like it :D.
Radarek
2007-11-03 14:10:46
@Daniel Berger


You probably tried something like this:


[code]
$KCODE = "u"


def ∑
#...
end
[/code]


It doesn't work of course. But try this:


[code]
#file: run.rb
$KCODE = "u"


require 'fun_with_utf'
[/code]


[code]
#file: fun_with_utf.rb


def ∑
#...
end


puts ∑(...)
[/code]


and run with command: ruby run.rb (without -Ku). It should work.


Explanation is very simple. In first example ruby try to parse source code, but see strange characters and raise errors. In second example ruby parses file "run.rb" and everything is ok. Next it executes file, variable's value $KCODE is changed to "u" so now it can handle utf. Next it load external file "fun_with_utf.rb", so ruby parses it. But now it can do it, because it can recognize characters like "∑".


See difference?

mccoyn
2007-11-04 06:47:18
Now we need an engineering keyboard with a row of common greek letters used in math.
Daniel Berger
2007-11-04 07:12:29
@radarak - Thanks, figured it was something like that.


@mccoyn - I've actually thought about that. Is there any sort of standard layout that's been discussed? I wonder if the Unicode Consortium has any thoughts.


gwern
2007-11-07 08:33:10
mccoyn: Yes! Then we'll be able to go back to APL!
Tim O'Brien
2007-11-07 09:42:53
This is brilliant!
georg
2007-12-18 07:29:57
> This is brilliant!


i think similar) very cool)

Carsten
2007-12-20 13:58:43
and open for other opportunities a plenty full bunch of ideas. Thanks for the UTF (32bit and higher :-)
Roger
2008-01-17 01:27:52
so amazing! it gives me a new view of unicode.
Carsten
2008-01-23 02:55:51
Great post,


would never have thought it would be that easy