Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Dan-Q
Created November 26, 2012 23:06
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Dan-Q/4151267 to your computer and use it in GitHub Desktop.
Save Dan-Q/4151267 to your computer and use it in GitHub Desktop.
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
Copy link

HansCz commented Mar 8, 2013

Wonderfully useless :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment