Skip to content

Instantly share code, notes, and snippets.

@Thermatix
Forked from Dan-Q/whitespace-a-like.rb
Created August 25, 2017 13:22
Show Gist options
  • Save Thermatix/bdeace3b38a85e39b1b0ff3fb30bc091 to your computer and use it in GitHub Desktop.
Save Thermatix/bdeace3b38a85e39b1b0ff3fb30bc091 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!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment