ruby-uebungen/Assignment_021/solution/readme.md

13 lines
516 B
Markdown
Raw Normal View History

2023-05-25 17:47:19 +02:00
# Lösung: Closure
2023-05-23 09:19:31 +02:00
<div style="border: 1px solid grey;"><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br></div>```ruby
2023-05-25 17:47:19 +02:00
def create_counter
n = 0 # closure
Proc.new { n += 1 }
2023-05-23 09:19:31 +02:00
end
2023-05-25 17:47:19 +02:00
p = create_counter
puts p.call # -> 1
puts p.call # -> 2
2023-05-23 09:19:31 +02:00
```