File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -574,6 +574,8 @@ var RUBY_EXAMPLES = {
574574 rubyPrivateProtectedLink : 'ruby-example-code/class-private-protected.rb' ,
575575 rubyInstClassVarsComplexLink : 'ruby-example-code/inst-class-vars-complex.rb' ,
576576 rubyToplevelLink : 'ruby-example-code/toplevel-inst-class-vars.rb' ,
577+ rubyBlocksScoping3Link : 'ruby-example-code/blocks-scoping-3.rb' ,
578+ rubyProcReturnLink : 'ruby-example-code/proc-return.rb' ,
577579} ;
578580
579581var chatBox = undefined ;
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 1+ # adapted from http://eli.thegreenplace.net/2006/04/18/understanding-ruby-blocks-procs-and-methods/
2+
3+ def gen_times ( factor )
4+ return Proc . new { |n | n *factor }
5+ end
6+
7+ times3 = gen_times ( 3 )
8+ times5 = gen_times ( 5 )
9+
10+ puts times3 . call ( 12 ) #=> 36
11+ puts times5 . call ( 5 ) #=> 25
Original file line number Diff line number Diff line change 392392 < a class ="exampleLink " id ="rubyGlobalsLink " href ="# "> Globals</ a > |
393393 < a class ="exampleLink " id ="rubyConstantsLink " href ="# "> Constants</ a > |
394394 < a class ="exampleLink " id ="rubyBlocksLink " href ="# "> Blocks</ a > |
395- < a class ="exampleLink " id ="rubyBlocksScopingLink " href ="# "> Block scoping</ a >
395+ < a class ="exampleLink " id ="rubyBlocksScopingLink " href ="# "> Block scoping</ a > |
396+ < a class ="exampleLink " id ="rubyBlocksScoping3Link " href ="# "> More block scoping</ a >
396397 < p />
397- < a class ="exampleLink " id ="rubyProcLink " href ="# "> Procs </ a > |
398+ < a class ="exampleLink " id ="rubyProcLink " href ="# "> Proc & Lambda </ a > |
398399 < a class ="exampleLink " id ="rubyProcScopingLink " href ="# "> Proc scoping</ a > |
400+ < a class ="exampleLink " id ="rubyProcReturnLink " href ="# "> Proc return</ a > |
399401 < a class ="exampleLink " id ="rubyLambdaScopingLink " href ="# "> Lambda scoping</ a > |
400402 < a class ="exampleLink " id ="rubyInheritanceLink " href ="# "> Inheritance</ a > |
401403 < a class ="exampleLink " id ="rubySymbolsLink " href ="# "> Symbols</ a >
You can’t perform that action at this time.
0 commit comments