8000 Add a new easter egg: dancing ruby · ruby/irb@e37dc7e · GitHub
[go: up one dir, main page]

Skip to content

Commit e37dc7e

Browse files
committed
Add a new easter egg: dancing ruby
1 parent 00304f7 commit e37dc7e

File tree

3 files changed

+139
-5
lines changed

3 files changed

+139
-5
lines changed

lib/irb.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
require "irb/color"
2222

2323
require "irb/version"
24+
require "irb/easter-egg"
2425

2526
# IRB stands for "interactive Ruby" and is a tool to interactively execute Ruby
2627
# expressions read from the standard input.

lib/irb/completion.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,7 @@ def self.retrieve_completion_data(input, bind: IRB.conf[:MAIN_CONTEXT].workspace
268268
PerfectMatchedProc = ->(matched) {
269269
RDocRIDriver ||= RDoc::RI::Driver.new
270270
if matched =~ /\A(?:::)?RubyVM/ and not ENV['RUBY_YES_I_AM_NOT_A_NORMAL_USER']
271-
File.open(File.join(__dir__, 'ruby_logo.aa')) do |f|
272-
RDocRIDriver.page do |io|
273-
IO.copy_stream(f, io)
274-
end
275-
end
271+
IRB.send(:easter_egg)
276272
return
277273
end
278274
namespace = retrieve_completion_data(matched, doc_namespace: true)

lib/irb/easter-egg.rb

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
require "reline"
2+
3+
module IRB
4+
class << self
5+
class Vec
6+
def initialize(x, y, z)
7+
@x, @y, @z = x, y, z
8+
end
9+
10+
attr_reader :x, :y, :z
11+
12+
def sub(other)
13+
Vec.new(@x - other.x, @y - other.y, @z - other.z)
14+
end
15+
16+
10000 def dot(other)
17+
@x*other.x + @y*other.y + @z*other.z
18+
end
19+
20+
def cross(other)
21+
ox, oy, oz = other.x, other.y, other.z
22+
Vec.new(@y*oz-@z*oy, @z*ox-@x*oz, @x*oy-@y*ox)
23+
end
24+
25+
def normalize
26+
r = Math.sqrt(self.dot(self))
27+
Vec.new(@x / r, @y / r, @z / r)
28+
end
29+
end
30+
31+
class Canvas
32+
def initialize((h, w))
33+
@data = (0..h-2).map { [0] * w }
34+
@scale = [w / 2.0, h-2].min
35+
@center = Complex(w / 2, h-2)
36+
end
37+
38+
def line((x1, y1), (x2, y2))
39+
p1 = Complex(x1, y1) / 2 * @scale + @center
40+
p2 = Complex(x2, y2) / 2 * @scale + @center
41+
line0(p1, p2)
42+
end
43+
44+
private def line0(p1, p2)
45+
mid = (p1 + p2) / 2
46+
if (p1 - p2).abs < 1
47+
x, y = mid.rect
48+
@data[y / 2][x] |= (y % 2 > 1 ? 2 : 1)
49+
else
50+
line0(p1, mid)
51+
line0(p2, mid)
52+
end
53+
end
54+
55+
def draw
56+
@data.each {|row| row.fill(0) }
57+
yield
58+
@data.map {|row| row.map {|n| " ',;"[n] }.join }.join("\n")
59+
end
60+
end
61+
62+
class RubyModel
63+
def initialize
64+
@faces = init_ruby_model
65+
end
66+
67+
def init_ruby_model
68+
cap_vertices = (0..5).map {|i| Vec.new(*Complex.polar(1, i * Math::PI / 3).rect, 1) }
69+
middle_vertices = (0..5).map {|i| Vec.new(*Complex.polar(2, (i + 0.5) * Math::PI / 3).rect, 0) }
70+
bottom_vertex = Vec.new(0, 0, -2)
71+
72+
faces = [cap_vertices]
73+
6.times do |j|
74+
i = j-1
75+
faces << [cap_vertices[i], middle_vertices[i], cap_vertices[j]]
76+
faces << [cap_vertices[j], middle_vertices[i], middle_vertices[j]]
77+
faces << [middle_vertices[i], bottom_vertex, middle_vertices[j]]
78+
end
79+
80+
faces
81+
end
82+
83+
def render_frame(i)
84+
angle = i / 10.0
85+
dir = Vec.new(*Complex.polar(1, angle).rect, Math.sin(angle)).normalize
86+
dir2 = Vec.new(*Complex.polar(1, angle - Math::PI/2).rect, 0)
87+
up = dir.cross(dir2)
88+
nm = dir.cross(up)
89+
@faces.each do |vertices|
90+
v0, v1, v2, = vertices
91+
if v1.sub(v0).cross(v2.sub(v0)).dot(dir) > 0
92+
points = vertices.map {|p| [nm.dot(p), up.dot(p)] }
93+
(points + [points[0]]).each_cons(2) do |p1, p2|
94+
yield p1, p2
95+
end
96+
end
97+
end
98+
end
99+
end
100+
101+
private def easter_egg(type = nil)
102+
type ||= [:logo, :dancing].sample
103+
case type
104+
when :logo
105+
File.open(File.join(__dir__, 'ruby_logo.aa')) do |f|
106+
require "rdoc"
107+
RDoc::RI::Driver.new.page do |io|
108+
IO.copy_stream(f, io)
109+
end
110+
end
111+
when :dancing
112+
begin
113+
canvas = Canvas.new(Reline.get_screen_size)
114+
Reline::IOGate.set_winch_handler do
115+
canvas = Canvas.new(Reline.get_screen_size)
116+
end
117+
ruby_model = RubyModel.new
118+
print "\e[?1049h"
119+
(0..).each do |i|
120+
buff = canvas.draw do
121+
ruby_model.render_frame(i) do |p1, p2|
122+
canvas.line(p1, p2)
123+
end
124+
end
125+
buff[0, 20] = "\e[0mPress Ctrl+C to stop\e[31m\e[1m"
126+
print "\e[H" + buff
127+
sleep 0.05
128+
end
129+
ensure
130+
print "\e[0m\e[?1049l"
131+
end
132+
end
133+
end
134+
end
135+
end
136+
137+
IRB.send(:easter_egg, ARGV[0]&.to_sym) if $0 == __FILE__

0 commit comments

Comments
 (0)
0