Skip to content

Commit 432c725

Browse files
committed
moar
1 parent 3eb97c9 commit 432c725

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# how does lexical scoping work for blocks?
2+
3+
x = 5
4+
5+
def gimme
6+
y = 10
7+
yield
8+
puts "You're welcome."
9+
end
10+
11+
gimme do
12+
puts x
13+
x += 100 # can modify x in enclosing scope
14+
puts x
15+
puts y # can't find y since this is lexically scoped
16+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# how does lexical scoping work for blocks?
2+
3+
x = 5
4+
5+
def gimme
6+
yield
7+
puts "end of gimme"
8+
end
9+
10+
def bar
11+
y = 42
12+
gimme do
13+
# everything inside of here can access y but *not* x
14+
puts 'Hello'
15+
puts y
16+
y += 1000
17+
puts y
18+
end
19+
puts 'end of bar'
20+
puts y
21+
end
22+
23+
bar
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# how does lexical scoping work for blocks?
2+
3+
x = 5
4+
5+
def gimme
6+
y = 10
7+
yield
8+
puts "Done!"
9+
end
10+
11+
gimme do
12+
puts x
13+
x += 100 # can modify x in enclosing scope
14+
puts x
15+
end
16+
17+
puts x

0 commit comments

Comments
 (0)