-
Notifications
You must be signed in to change notification settings - Fork 0
/
same_time_same_place.rb
83 lines (66 loc) · 2.11 KB
/
same_time_same_place.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
#!/usr/bin/env ruby -w
# same_time_same_place.rb
class SameTimeSamePlace
EPISODE_NAME = 'Same Time, Same Place'
=begin rdoc
This Hash holds various procedure objects. One is formed by the generally
preferred Kernel.lambda method. Others are created with the older Proc.new
method, which has the benefit of allowing more flexibility in its argument
stack.
=end
QUESTIONS = {
:ternary => Proc.new do |args|
state = args ? args[0] : 'what'
location = args ? args[1] : 'what'
"Spike's #{state} in the #{location}ment?"
end,
:unless0th => Proc.new do |*args|
args = %w/what what/ unless args[0]
"Spike's #{args[0]} in the #{args[1]}ment?"
end,
:nitems => Proc.new do |*args|
args.nitems >= 2 || args.replace(['what', 'what'])
"Spike's #{args[0]} in the #{args[1]}ment?"
end,
:second_or => Proc.new do |*args|
args[1] || args.replace(['what', 'what'])
"Spike's #{args[0]} in the #{args[1]}ment?"
end,
:needs_data => lambda do |args|
"Spike's #{args[0]} in the #{args[1]}ment?"
end
}
DATA_FROM_ANYA = ['insane', 'base']
def SameTimeSamePlace.describe()
same_as_procs = [
SameTimeSamePlace.yield_block(&QUESTIONS[:nitems]),
QUESTIONS[:second_or].call(),
QUESTIONS[:unless0th].call(),
SameTimeSamePlace.willow_ask,
]
return <<DONE
In #{EPISODE_NAME},
Willow asks "#{QUESTIONS[:ternary].call(nil)}",
#{same_as_procs.map do |proc_output|
'which is the same as "' + proc_output + '"'
end.join("\n ")
}
Anya provides "#{DATA_FROM_ANYA.join(', ')}", which forms the full question
"#{SameTimeSamePlace.yield_block(DATA_FROM_ANYA, &QUESTIONS[:needs_data])}".
DONE
end
=begin rdoc
Wrapping a lambda call within a function can provide
default values for arguments
=end
def SameTimeSamePlace.willow_ask(args = ['what', 'what'])
QUESTIONS[:needs_data][args]
end
=begin rdoc
Passing a block as an argument to a method
=end
def SameTimeSamePlace.yield_block(*args, &block)
# yield with any necessary args is the same as calling block.call(*args)
yield(*args)
end
end