-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14.1.proc_example.rb
More file actions
71 lines (53 loc) · 1.34 KB
/
14.1.proc_example.rb
File metadata and controls
71 lines (53 loc) · 1.34 KB
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
def dividers
puts ''
puts '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
puts ' Next Example'
puts '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++'
puts ''
end
def do_self_importantly some_proc
puts "Everybody just HOLD ON! I'm doing something..."
some_proc.call
puts "OK everyone, I'm done. As you were."
end
say_hello = Proc.new do
puts 'hello'
end
do_self_importantly say_hello
dividers
puts 'Proc example extended.'
do_you_like = Proc.new do |good_stuff|
puts "I *really* like #{good_stuff}"
end
do_you_like.call 'chocolate'
do_you_like.call 'Ruby'
dividers
puts 'Proc example extended.'
def do_until_false first_input, some_proc
input = first_input
output = first_input
while output
input = output
output = some_proc.call input
end
input
end
build_array_of_squares = Proc.new do |array|
last_number = array.last
if last_number <= 0
false
else
# Take off the last number...
array.pop
# ... and replace it with its square...
array.push last_number * last_number
# ...followed by the next smaller number.
array.push last_number - 1
end
end
always_false = Proc.new do |just_ignore_me|
false
end
puts do_until_false([5], build_array_of_squares).inspect
yum = 'lemonade with a hint of orange blossom water'
puts do_until_false(yum, always_false)