-
Notifications
You must be signed in to change notification settings - Fork 0
/
runCode.rb
37 lines (32 loc) · 968 Bytes
/
runCode.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
code_area = JS.global.document.getElementById("ruby-code")
@window = JS.global
@window.editor = @window[:CodeMirror].fromTextArea(code_area, {
lineNumbers: true,
mode: "text/x-ruby",
matchBrackets: true,
indentUnit: 4
})
JS.global.document.getElementById("spinner").style.display = "none"
define_method :click_run_button do
code_area = JS.global.document.getElementById("ruby-code")
@window.editor.save
output_div = JS.global.document.getElementById("output")
code = <<-RUBY
#{code_area.value}
RUBY
begin
output_div.innerHTML = Kernel.eval(code.strip)
rescue => e
output_div.innerHTML = e.to_s
end
end
# check_interval = JS.global.setInterval(lambda {
# if @window.rubyVM != undefined
# JS.global.clearInterval(check_interval)
# click_run_button
# end
# }, 500)
# Set up the event listener for the run code button
JS.global.document.getElementById("run-code").addEventListener("click") do |e|
click_run_button
end