File tree Expand file tree Collapse file tree
v4-cokapi/backends/ruby/tests Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments