/
_evaluator.rb
executable file
·52 lines (45 loc) · 1.17 KB
/
_evaluator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def compute
@code = $d.code.value.to_s
@result = eval(@code, console_binding) unless @code.empty?
rescue => e
@result = e.class
ensure
show_result
@code = @result = nil
end
def show_result
$d.result.append(tag("span", :id => 'prompt') { "» " })
$d.result.append(@code.empty? ? tag("br") : (tag("span"){ @code } + tag("div"){ @result.inspect }))
$d.code.value = ""
end
def move_on
@instructions ||= nil
@instructions.nil? ? get_instructions : next_instruction
end
def get_instructions(&block)
download "/tryruby/instructions.json" do |s, a|
@instructions = JSON.parse a.result
move_on
end
end
def next_instruction
@current ||= 0
$d.instructions.html = @instructions[@current].to_s
@current += 1 if @current < @instructions.size - 1
end
def hook_run
$d.run.onclick do |s, a|
document.loading.html = tag('img', :src => '/images/loadinfo.net.gif', :alt => 'evaluating ...')
compute
move_on
document.loading.html = ''
HtmlPage.window.eval "moveTo('console', 'code')"
end
end
def console_binding
binding
end
$d.code.value = ''
$d.code.focus
hook_run
get_instructions