Skip to content

Commit f1e3f92

Browse files
committed
bah
1 parent 432c725 commit f1e3f92

4 files changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# how does lexical scoping work for lambdas?
2+
3+
x = 5
4+
5+
p = lambda {
6+
puts x
7+
x += 100 # can modify x in enclosing scope
8+
puts x
9+
puts 'end of p'
10+
}
11+
12+
def gimme
13+
x = 10
14+
print 'In gimme, x is '
15+
puts x
16+
yield
17+
puts "You're welcome."
18+
print 'In gimme, x is '
19+
puts x
20+
end
21+
22+
gimme &p # use '&' to turn a Proc into a block
23+
print 'In main, x is '
24+
puts x
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# how does lexical scoping work for lambdas?
2+
3+
x = 5
4+
5+
p = lambda {
6+
puts x
7+
x += 100 # can modify x in enclosing scope
8+
puts x
9+
puts 'end of p'
10+
}
11+
12+
p.call
13+
puts x
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# how does lexical scoping work for Procs?
2+
3+
x = 5
4+
5+
p = Proc.new do
6+
puts x
7+
x += 100 # can modify x in enclosing scope
8+
puts x
9+
puts 'end of p'
10+
end
11+
12+
def gimme
13+
x = 10
14+
print 'In gimme, x is '
15+
puts x
16+
yield
17+
puts "You're welcome."
18+
print 'In gimme, x is '
19+
puts x
20+
end
21+
22+
gimme &p # use '&' to turn a Proc into a block
23+
print 'In main, x is '
24+
puts x
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# how does lexical scoping work for Procs?
2+
3+
x = 5
4+
5+
p = Proc.new do
6+
puts x
7+
x += 100 # can modify x in enclosing scope
8+
puts x
9+
puts 'end of p'
10+
end
11+
12+
p.call
13+
puts x

0 commit comments

Comments
 (0)