Skip to content

Commit 26438b5

Browse files
committed
added ruby examples
1 parent 5f1c979 commit 26438b5

15 files changed

Lines changed: 250 additions & 1 deletion

v3/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
<head>
3333

34-
<title>Online Python Tutor - Visualize Python, Java, JavaScript, TypeScript, and Ruby code</title>
34+
<title>Online Python Tutor - Visualize Python, Java, JavaScript, TypeScript, and Ruby code execution</title>
3535

3636
<meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
3737

v3/js/opt-frontend.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,21 @@ var PY3_EXAMPLES = {
559559
nonlocalLink: "example-code/nonlocal.txt",
560560
}
561561

562+
var RUBY_EXAMPLES = {
563+
rubyBlocksLink: 'ruby-example-code/blocks-basic.rb',
564+
rubyBlocksScopingLink: 'ruby-example-code/blocks-scoping-2.rb',
565+
rubyInheritanceLink: 'ruby-example-code/class-inheritance.rb',
566+
rubyConstantsLink: 'ruby-example-code/constants-4.rb',
567+
rubyContainersLink: 'ruby-example-code/container-data-types.rb',
568+
rubyGlobalsLink: 'ruby-example-code/globals.rb',
569+
rubyLambdaScopingLink: 'ruby-example-code/lambda-scoping-2.rb',
570+
rubyMegagreeterLink: 'ruby-example-code/megagreeter.rb',
571+
rubyProcLink: 'ruby-example-code/proc-basic.rb',
572+
rubyProcScopingLink: 'ruby-example-code/proc-scoping.rb',
573+
rubySymbolsLink: 'ruby-example-code/symbols.rb',
574+
rubyToplevelLink: 'ruby-example-code/toplevel-inst-class-vars.rb',
575+
};
576+
562577
var chatBox = undefined;
563578
function createChatBox() {
564579
assert(!chatBox);
@@ -639,6 +654,9 @@ $(document).ready(function() {
639654
} else if (JAVA_EXAMPLES[myId] !== undefined) {
640655
exFile = JAVA_EXAMPLES[myId];
641656
lang = 'java';
657+
} else if (RUBY_EXAMPLES[myId] !== undefined) {
658+
exFile = RUBY_EXAMPLES[myId];
659+
lang = 'ruby';
642660
} else if (PY2_EXAMPLES[myId] !== undefined) {
643661
exFile = PY2_EXAMPLES[myId];
644662

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Adapted from Learning Ruby by Michael Fitzgerald, O'Reilly
2+
3+
def gimme
4+
if block_given?
5+
yield
6+
else
7+
print "Oops, no block."
8+
end
9+
puts " You're welcome."
10+
end
11+
12+
gimme { print "Thank you." }
13+
14+
gimme do
15+
localX = "Thank you again."
16+
print localX
17+
end
18+
19+
gimme # no block
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# how does lexical scoping work for blocks?
2+
3+
x = 5
4+
5+
def gimme
6+
y = 10
7+
yield
8+
puts "You're welcome."
9+
end
10+
11+
gimme do
12+
puts x
13+
x += 100 # can modify x in enclosing scope
14+
puts x
15+
puts y # can't find y since this is lexically scoped
16+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# adapted from http://rubylearning.com/satishtalim/ruby_inheritance.html
2+
class Mammal
3+
def breathe
4+
puts "inhale and exhale"
5+
end
6+
end
7+
8+
class Cat < Mammal
9+
def speak
10+
puts "Meow"
11+
end
12+
end
13+
14+
class Tabby < Cat
15+
def speak
16+
puts "Tabby meow"
17+
end
18+
end
19+
20+
rani = Cat.new
21+
rani.breathe
22+
rani.speak
23+
24+
tab = Tabby.new
25+
tab.speak
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# adapted from http://rubylearning.com/satishtalim/ruby_constants.html
2+
3+
OUTER_CONST = 99
4+
5+
class Const
6+
def get_const
7+
CONST
8+
end
9+
CONST = OUTER_CONST + 1
10+
end
11+
12+
puts Const.new.get_const
13+
puts Const::CONST
14+
puts ::OUTER_CONST
15+
puts Const::NEW_CONST = 123
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'set'
2+
3+
my_array = [1, 2, 3, 'hello', false, true, nil]
4+
my_hash = {'John' => 26, 'Jane' => 21, 'Jack' => 30}
5+
my_set = my_array.to_set
6+
7+
my_hash['nested array'] = my_array
8+
my_array[1] = ['a', 'b', 'c']

v3/ruby-example-code/globals.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
$globalX = 'I am a global!'
2+
$globalY = 'I am another global!'
3+
4+
localX = 'I am a local in <main>'
5+
6+
def foo
7+
reallyLocalX = 'I am a local in foo'
8+
puts reallyLocalX
9+
$globalX << $globalY
10+
end
11+
12+
$globalX << '---'
13+
foo
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 lambda into a block
23+
print 'In main, x is '
24+
puts x
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Adapted from Ruby in Twenty Minutes
2+
# https://www.ruby-lang.org/en/documentation/quickstart/3/
3+
4+
class MegaGreeter
5+
attr_accessor :names
6+
7+
# Create the object
8+
def initialize(names = "World")
9+
@names = names
10+
end
11+
12+
# Say hi to everybody
13+
def say_hi
14+
if @names.nil?
15+
puts "..."
16+
elsif @names.respond_to?("each")
17+
# @names is a list of some kind, iterate!
18+
@names.each do |name|
19+
puts "Hello #{name}!"
20+
end
21+
else
22+
puts "Hello #{@names}!"
23+
end
24+
end
25+
end
26+
27+
28+
mg = MegaGreeter.new
29+
mg.say_hi
30+
31+
# Change name to be "Zeke"
32+
mg.names = "Zeke"
33+
mg.say_hi
34+
35+
# Change the name to an array of names
36+
mg.names = ["Albert", "Brenda", "Charles",
37+
"Dave", "Engelbert"]
38+
mg.say_hi
39+
40+
# Change to nil
41+
mg.names = nil
42+
mg.say_hi

0 commit comments

Comments
 (0)