Skip to content

@avapoet /whitespace-a-like.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Encode a Ruby program into a version composed almost entirely of unicode whitespace characters. Decodes itself on the fly.
#!/usr/bin/env ruby
# encoding: utf-8
CHARS = %w{                       ​ ‌ ‍    }
def encode(string)
string.chars.map{|c|"#{CHARS[c[0]/16]}#{CHARS[c[0]%16]}"}.join
end
program = <<-EOF
what_to_say = 'Hello World!'
puts "This program will say \\"\#{what_to_say}\\" ten times:"
10.times{puts what_to_say}
EOF
File::open('ciphered.rb', 'w') do |f|
f.puts "#!/usr/bin/env ruby\n# encoding: utf-8\nh=Hash[%w{                       ​ ‌ ‍    }.zip((0..15).to_a)]"
f.puts "eval('#{encode(program)}'.chars.each_slice(3).map{|c|h[c.join]}.each_slice(2).map{|a|((a[0]*16)+a[1]).chr}.join)"
end
# sample output written into ciphered.rb:
#
# #!/usr/bin/env ruby
# # encoding: utf-8
# h=Hash[%w{                       ​ ‌ ‍    }.zip((0..15).to_a)]
# eval('                         ‍         ‌ ‌           ‌                                           ‍       ‌ ‌           ‌     ​                       ‍ ‌                 ‍                     ‍     ​                                 ‍  '.chars.each_slice(3).map{|c|h[c.join]}.each_slice(2).map{|a|((a[0]*16)+a[1]).chr}.join)
#
# sample output of running ciphered.rb:
#
# This program will say "Hello World!" ten times:
# Hello World!
# Hello World!
# Hello World!
# Hello World!
# Hello World!
# Hello World!
# Hello World!
# Hello World!
# Hello World!
# Hello World!
@HansCz

Wonderfully useless :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.