Skip to content

Commit 866c3bb

Browse files
committed
moar examples
1 parent 7f76f91 commit 866c3bb

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

v3/js/opt-frontend.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ var RUBY_EXAMPLES = {
571571
rubyProcLink: 'ruby-example-code/proc-basic.rb',
572572
rubyProcScopingLink: 'ruby-example-code/proc-scoping.rb',
573573
rubySymbolsLink: 'ruby-example-code/symbols.rb',
574+
rubyPrivateProtectedLink: 'ruby-example-code/class-private-protected.rb',
575+
rubyInstClassVarsComplexLink: 'ruby-example-code/inst-class-vars-complex.rb',
574576
rubyToplevelLink: 'ruby-example-code/toplevel-inst-class-vars.rb',
575577
};
576578

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class MyClass
2+
def foo; end # instance method
3+
def self.foo; end # class method
4+
5+
protected
6+
7+
def bar; end
8+
def self.bar; end # NOP!
9+
10+
# variables shouldn't be affected
11+
@@classVarX = 111
12+
@instVarX = 222
13+
14+
private
15+
16+
@@classVarY = 333
17+
@instVarY = 444
18+
19+
def baz; end
20+
def self.baz; end # NOP!
21+
22+
# dynamically change visibility
23+
public :baz
24+
private :bar
25+
private :foo
26+
27+
private_class_method :foo
28+
private_class_method :bar
29+
private_class_method :baz
30+
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# adapted from http://rubylearning.com/satishtalim/ruby_constants.html
2+
3+
# p057mymethods2.rb
4+
# variables and methods start lowercase
5+
$glob = 5 # global variables start with $
6+
class TestVar # class name constant, start uppercase
7+
@@cla = 6 # class variables start with @@
8+
CONST_VAL = 7 # constant style, all caps, underscore
9+
def initialize(x) # constructor
10+
@inst = x # instance variables start with @
11+
@@cla += 1 # each object shares @@cla
12+
end
13+
def self.cla # class method, getter
14+
@@cla
15+
end
16+
def self.cla=(y) # class method, setter, also TestVar.
17+
@@cla = y
18+
end
19+
def inst # instance method, getter
20+
@inst
21+
end
22+
def inst=(i) # instance method, setter
23+
@inst = i
24+
end
25+
end
26+
puts $glob
27+
test = TestVar.new(3) # calls constructor
28+
puts TestVar.cla # calls getter
29+
puts test.inspect # gives object ID and instance vars
30+
TestVar.cla = 4 # calls setter
31+
test.inst=8 # calls setter
32+
puts TestVar.cla
33+
puts test.inst # calls getter
34+
other = TestVar.new(17)
35+
puts other.inspect
36+
puts TestVar.cla

v3/visualize.html

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -390,17 +390,20 @@
390390
<p style="margin-top: 5px;">
391391
<a class="exampleLink" id="rubyContainersLink" href="#">Containers</a> |
392392
<a class="exampleLink" id="rubyGlobalsLink" href="#">Globals</a> |
393-
<a class="exampleLink" id="rubyToplevelLink" href="#">Toplevel</a> |
394393
<a class="exampleLink" id="rubyConstantsLink" href="#">Constants</a> |
395394
<a class="exampleLink" id="rubyBlocksLink" href="#">Blocks</a> |
396-
<a class="exampleLink" id="rubyBlocksScopingLink" href="#">Blocks scoping</a>
395+
<a class="exampleLink" id="rubyBlocksScopingLink" href="#">Block scoping</a>
397396
<p/>
398397
<a class="exampleLink" id="rubyProcLink" href="#">Procs</a> |
399398
<a class="exampleLink" id="rubyProcScopingLink" href="#">Proc scoping</a> |
400399
<a class="exampleLink" id="rubyLambdaScopingLink" href="#">Lambda scoping</a> |
401400
<a class="exampleLink" id="rubyInheritanceLink" href="#">Inheritance</a> |
402-
<a class="exampleLink" id="rubyMegagreeterLink" href="#">Megagreeter</a> |
403401
<a class="exampleLink" id="rubySymbolsLink" href="#">Symbols</a>
402+
<p/>
403+
<a class="exampleLink" id="rubyPrivateProtectedLink" href="#">Protected & private</a> |
404+
<a class="exampleLink" id="rubyInstClassVarsComplexLink" href="#">Class & instance vars</a> |
405+
<a class="exampleLink" id="rubyToplevelLink" href="#">Toplevel defs</a> |
406+
<a class="exampleLink" id="rubyMegagreeterLink" href="#">Megagreeter</a>
404407
</p>
405408

406409
</div>
@@ -478,7 +481,7 @@
478481
href="https://github.com/pgbovine/OnlinePythonTutor/tree/master/v4-cokapi/backends/javascript">Backend
479482
source code</a>.</p>
480483

481-
<p>5. Ruby 2.2.2 (MRI). <a
484+
<p>5. Ruby 2 using MRI 2.2.2. <a
482485
href="https://github.com/pgbovine/OnlinePythonTutor/tree/master/v4-cokapi/backends/ruby">Backend
483486
source code</a>.</p>
484487

0 commit comments

Comments
 (0)