Created
November 26, 2014 11:08
-
-
Save deiwin/d518e8109c3aeb4fa979 to your computer and use it in GitHub Desktop.
Whitespace translator
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
#!/bin/ruby | |
SPACE = "S " | |
TAB = "T\t" | |
NEWLINE = "L\n" | |
def to_binary(c) | |
return c.to_s(2) | |
end | |
def to_ws_binary(b) | |
return SPACE + b.gsub("1", TAB).gsub("0", SPACE) + NEWLINE | |
end | |
def to_ws_print_command(b) | |
return SPACE + SPACE + b + TAB + NEWLINE + SPACE + SPACE | |
end | |
def add_code_end(c) | |
return c + NEWLINE * 3 | |
end | |
def s_to_ws(s) | |
print_commands = s.each_byte.map do |c| | |
char_as_binary_string = to_binary(c) | |
char_as_ws_binary = to_ws_binary(char_as_binary_string) | |
ws_print_command = to_ws_print_command(char_as_ws_binary) | |
end.reduce(:+) | |
code = add_code_end(print_commands) | |
return code | |
end | |
puts s_to_ws("Hello, World!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool