-
Notifications
You must be signed in to change notification settings - Fork 2
/
nudge_stack.rb
86 lines (68 loc) · 1.41 KB
/
nudge_stack.rb
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
# encoding: UTF-8
class NudgeStack < Array
def initialize (value_type)
@value_type = value_type
@items_pushed = 0
end
def items_pushed
@items_pushed
end
def << (item)
@items_pushed += 1
push item.to_s
end
alias depth length
alias flush clear
alias pop_string pop
private :push, :pop
public :pop_string
def pop_value
if value = pop
value.send(:"to_#{@value_type}")
end
end
def shove (n)
return if length < 2
case
when n <= 0
when n >= length then unshift(pop)
else insert(length - 1 - n, pop)
end
end
def yank (n)
return if length < 2
case
when n <= 0
when n >= length then push(shift)
else push(delete_at(length - 1 - n))
end
end
def yankdup (n)
return if length < 2
case
when n <= 0 then push(last)
when n >= length then push(first)
else push(self[length - 1 - n])
end
end
end
class ExecStack < NudgeStack
alias << push
alias pop_value pop
undef pop_string
public :<<, :pop_value
def yankdup (n)
return if length < 2
case
when n <= 0 then push(last.dup)
when n >= length then push(first.dup)
else push(self[length - 1 - n].dup)
end
end
end
class ErrorStack < NudgeStack
def << (error)
push "#{error.class.name.split('::').last}: #{error.message}"
end
undef pop_value
end