aanand (owner)

Revisions

gist: 14906 Download_button fork
public
Public Clone URL: git://gist.github.com/14906.git
Embed All Files: show embed
Analog Literals #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# see http://www.xs4all.nl/~weegen/eelis/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