【发布时间】:2012-10-27 22:49:51
【问题描述】:
您好,我是 Rails 的新手,并且在 ruby 方面有几天经验。我使用线程创建了一个秒表程序。但是现在我想把它放在我的 Rails 应用程序中,并且视图应该有按钮来开始、停止、恢复秒表,因为我正在跟踪一个事件。有人可以展示如何将 ruby 程序嵌入到 rails 并从视图中使用它吗?
下面的程序应该从视图中的开始/停止/恢复按钮调用
stopwatch.rb:(目前只有start方法和pause方法)
class Stopwatch
def initialize
@t_start
@t_elapsed
@t_total
@t_current
end
def start
@t_start = Time.now
t = Thread.new do
while true do
@t_current = Time.now
puts @t_current
@t_elapsed = @t_current - @t_start
puts "Time elapsed is : #{@t_elapsed.to_i} seconds"
sleep 1
end
end
end
def pause
# Stop the Thread => Sleep the Thread until Resume is pressed.
t.kill
# Calculate the elapsed time
@t_elapsed = @t_current - @t_start
end
end
ticker = Stopwatch.new
ticker.start
谢谢,
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3