Dreaming in Code

By Phil Matarese on April 21st, 2008

Tagged with: ruby, frivolity

I dreamed about Ruby again last night.  I was looking at the way code looked, and I realized that the results of executing the code looked like the code that was being executed.  It went on recursively with code that looked like code that looked like code...  There was even drop-shadows and reflection on the code - it was beautiful.

I'm not sure if this is a result of reading Why's (Poignant) Guide to Ruby or if I'm just wandering down the same path he did.  It doesn't really matter though, in fact, the only thing that matters now is creating Ascii Art numbers.  This is important, I must do this...

  1  class NumberWang
2 ALL = %w{ zero one two three four five six seven eight
nine
}
3
4 # Omphaloskepsis
5 def self.reveal_method(method_name = nil)
6 @@source ||= File.open(__FILE__).read
7
8 source = ''
9
10 if method_name
11 in_method = false
12 @@source.each do |s|
13 next unless in_method || s.strip == "def
self.#{method_name}
"
14 break if s.strip == "end"
15 in_method = true
16
17 source << s
18 end
19 else
20 ALL.each{|a| reveal_method(a)}
21 end
22
23 puts source.chop #drop the linebreak
24 puts
25 end
26
27 def self.all
28 ALL.map{|a| self.send(a.to_sym)}
29 end
30
31 def self.zero
32 ----------1*1-------------
33 ---------1 - 1------------
34 ---------1 * 1------------
35 ---------1 - 1------------
36 ----------1*1------------1
37 end
38
39 def self.one
40 ------------1--------------
41 -----------11--------------
42 ------------1--------------
43 ------------1--------------
44 ----------11-1-------------1
45 end
46
47 def self.two
48 ----------1-1--------------
49 -------------1-------------
50 -----------11--------------
51 ----------1----------------
52 ----------1-11------------1
53 end
54
55 def self.three
56 ----------1--1------------
57 --------------1-----------
58 -----------1-1------------
59 --------------1-----------
60 ----------1--1-----------1
61 end
62
63 def self.four
64 ----------1--1------------
65 ----------1--1------------
66 ----------1-1-1-----------
67 -------------1------------
68 -------------1------------1
69 end
70
71 def self.five
72 ----------11-1-------------
73 ----------1----------------
74 -----------1-1-------------
75 --------------1------------
76 -----------1-1------------1
77 end
78
79 def self.six
80 ----------11*1-------------
81 ---------1-----------------
82 ---------1*-1--------------
83 ---------1 - 1-------------
84 ----------1-1-------------1
85 end
86
87 def self.seven
88 ----------11-1-------------
89 -------------1-------------
90 ------------1--------------
91 -----------1---------------
92 ----------1---------------1
93 end
94
95 def self.eight
96 ----------11*1------------
97 ---------1 - 1------------
98 ----------1-1-------------
99 ---------1 - 1------------
100 ----------1-1-------------1
101 end
102
103 def self.nine
104 -----------1*1------------
105 ----------1 - 1-----------
106 -----------1*11-----------
107 -------------1------------
108 -----------1-------------1
109 end
110 end

Ok, that was a strange diversion.  Sorry if I've upset anyone.  I'll try to stay a little more focused from now on.

Wrapping Up

There's some fun stuff in there (apart from the numbers) that might be worth poking around in.  Specifically, the reveal_method, which I think is too much of a hack to be useful but was fun to play with nonetheless.  Now, load this thing in irb and have some fun exploring.

curl 'http://rubyforge.org/snippet/download.php?type=snippet&id=337' > number_wang.rb
irb -r number_wang.rb

Try something like this, NumberWang.reveal_method :two to get you started.

Comments

Weigh in

(HTML tags are stripped, URLs are automatically converted to links)