Created
November 20, 2010 02:14
-
-
Save jgn/707543 to your computer and use it in GitHub Desktop.
rad50
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# http://en.wikipedia.org/wiki/RADIX-50 | |
class String | |
R50_CHARS = " ABCDEFGHIJKLMNOPQRSTUVWXYZ$.%0123456789" | |
R50_LOOKUP = {}.tap { |h| String::R50_CHARS.split(//).each_with_index { |c, i| h[c] = i } } | |
R50_CLEAN = /[[:upper:]|[:digit:]| |.|$|%]/ | |
def radix50 | |
[].tap do |r| | |
self.upcase.scan(R50_CLEAN).inject([]) do |m, c| | |
m << R50_LOOKUP[c] | |
end.each_slice(3) { |x, y, z| r << (((x || 0)*0o50 + (y || 0)) * 0o50 + (z || 0)).to_s(8) } | |
end | |
end | |
end | |
p 'Nothing sucks like a Vax.'.radix50 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment