ruby-uebungen/Assignment_021/solution/readme.md

13 lines
516 B
Markdown

# Lösung: Closure
<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
def create_counter
n = 0 # closure
Proc.new { n += 1 }
end
p = create_counter
puts p.call # -> 1
puts p.call # -> 2
```