Skip to content

Instantly share code, notes, and snippets.

@deiwin
Created November 26, 2014 11:08
Show Gist options
  • Save deiwin/d518e8109c3aeb4fa979 to your computer and use it in GitHub Desktop.
Save deiwin/d518e8109c3aeb4fa979 to your computer and use it in GitHub Desktop.
Whitespace translator
#!/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!")
@CubeFlix
Copy link

cool

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