Recursion using a Proc
(f=proc{|x,y|
sleep 1
system 'clear'
puts x
f[…]
})[…]
Slide 12
Slide 12 text
>> f = proc { |a,b| puts a }
=> #
>> f['hello']
hello
=> nil
Calling a Proc
Slide 13
Slide 13 text
>> f = proc { |a,b| puts a }
=> #
>> f['hello']
hello
=> nil Arguments that were
not provided are initialised as nil
(not the case for a lambda)
Calling a Proc
Slide 14
Slide 14 text
>> f = proc { |a,b| puts a }
=> #
>> f['hello']
hello
=> nil Arguments that were
not provided are initialised as nil
(not the case for a lambda)
Calling a Proc
Slide 15
Slide 15 text
First time we call it…
'~/\\'+'~'*12+'o~~'
Slide 16
Slide 16 text
'~/\\'+'~'*12+'o~~'
=> '~/\~~~~~~~~~~~~o~~'
First time we call it…
Slide 17
Slide 17 text
On every call from now on
we will do two things
1. Move the shark
2. Change the arms
Slide 18
Slide 18 text
Moving the shark
x.gsub(/\/\\./,'~/\\')
Slide 19
Slide 19 text
Moving the shark
x.gsub(/\/\\./,'~/\\')
Slide 20
Slide 20 text
Moving the shark
x.gsub(/\/\\./,'~/\\')
/\
Slide 21
Slide 21 text
Moving the shark
x.gsub(/\/\\./,'~/\\')
Slide 22
Slide 22 text
Moving the shark
x.gsub(/\/\\./,'~/\\')
remove one character
from the right
Slide 23
Slide 23 text
Moving the shark
x.gsub(/\/\\./,'~/\\')
Slide 24
Slide 24 text
Moving the shark
x.gsub(/\/\\./,'~/\\')
add one wave
to the left
Slide 25
Slide 25 text
~/\~~~~~~~~~~~~~~~
~~/\~~~~~~~~~~~~~~
~~~/\~~~~~~~~~~~~~
~~~~/\~~~~~~~~~~~~
Moving the shark
Slide 26
Slide 26 text
Changing the arms
.gsub(/.o./,y ? '\o/':'|o|')
Slide 27
Slide 27 text
matches all stages
~o~ \o/ |o|
of the animation
Changing the arms
.gsub(/.o./,y ? '\o/':'|o|')
Slide 28
Slide 28 text
depending on y
we change it to
either \o/ or |o|
Changing the arms
.gsub(/.o./,y ? '\o/':'|o|')
Slide 29
Slide 29 text
Changing the arms
(f=proc{|x,y|
sleep 1
system 'clear'
puts x
f[…,!y]
})[…]
There is no item 0,
does not matter
'Clean the house'.to_i #=> 0
'12'.to_i #=> 12
'0'.to_i #=> 0
Slide 37
Slide 37 text
todo_list = []
loop do
input_as_string = gets
input_as_int = input_as_string.to_i
system "clear"
if input_as_int > 0
todo_list.delete_at(input_as_int - 1)
else
todo_list << input_as_string
end
todo_list.each_with_index do |item, index|
puts "#{index+1}: #{item}"
end
end