Skip to content

Commit 004e8ee

Browse files
committed
bam
1 parent 49a890b commit 004e8ee

4 files changed

Lines changed: 40 additions & 2 deletions

File tree

v3/js/opt-frontend.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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

579581
var chatBox = undefined;
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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

v3/visualize.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,12 @@
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>

0 commit comments

Comments
 (0)