Last active
August 29, 2015 16:05
-
-
Save aanand/14906 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# see http://nullroute.eu.org/mirrors/analogliterals/analogliterals.xhtml | |
module AnalogLiterals | |
class A < Struct.new(:dashes, :tildes, :pluses, :els) | |
def -@; A.new(dashes+1, tildes, pluses, els); end | |
def ~@; A.new(dashes, tildes+1, pluses, els); end | |
def +@; A.new(dashes, tildes, pluses+1, els); end | |
def -x; A.new(dashes+x.dashes+1, tildes+x.tildes, pluses+x.pluses, els+x.els); end | |
def +x; A.new(dashes+x.dashes, tildes+x.tildes, pluses+x.pluses+1, els+x.els); end | |
def length; (dashes-1)/2; end | |
def width; (dashes-1)/2; end | |
def height | |
if els > 0 | |
pluses + (tildes-pluses-1)/3 | |
else | |
(tildes+pluses)/2 | |
end | |
end | |
def depth; els; end | |
end | |
module Helpers | |
I = A.new(0, 0, 0, 0) | |
def o; A.new(0, 0, 0, 0); end | |
L = A.new(0, 0, 0, 1) | |
end | |
end | |
if __FILE__ == $0 | |
require 'test/unit' | |
class AnalogLiteralsTest < Test::Unit::TestCase | |
def test_line | |
assert_equal 0, (I-I).length | |
assert_equal 1, (I---I).length | |
assert_equal 2, (I-----I).length | |
assert_equal 3, (I-------I).length | |
end | |
def test_rect2x3 | |
assert_equal 2, rect2x3.width | |
assert_equal 3, rect2x3.height | |
end | |
def test_cuboid6x6x3 | |
assert_equal 6, cuboid6x6x3.width | |
assert_equal 6, cuboid6x6x3.height | |
assert_equal 3, cuboid6x6x3.depth | |
end | |
def test_cuboid3x4x2 | |
assert_equal 3, cuboid3x4x2.width | |
assert_equal 4, cuboid3x4x2.height | |
assert_equal 2, cuboid3x4x2.depth | |
end | |
include AnalogLiterals::Helpers | |
def rect2x3 | |
o-----o | |
+ ~ | |
~ ~ | |
~ ~ | |
o-----o | |
end | |
def cuboid6x6x3 | |
o-------------o | |
+L \ | |
+ L \ | |
+ L \ | |
+ o-------------o | |
+ ~ ~ | |
~ ~ ~ | |
o + ~ | |
L + ~ | |
L + ~ | |
L+ ~ | |
o-------------o | |
end | |
def cuboid3x4x2 | |
o-------o | |
+L \ | |
+ L \ | |
+ o-------o | |
+ ~ ~ | |
o + ~ | |
L + ~ | |
L+ ~ | |
o-------o | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment