秒参数传递到方法并存储在哪里?将秒转换为字符串00:00:00

时间:2013-06-10 03:48:15

标签: ruby timer string-formatting instance-variables datetime-conversion

我收到错误:

Timer
  should initialize to 0 seconds
  time_string
      should display 0 seconds as 00:00:00 (FAILED - 1)

 Failures:

  1) Timer time_string should display 0 seconds as 00:00:00
     Failure/Error: @timer.seconds = 0
     NoMethodError:
       undefined method `seconds=' for #<Timer:0x007fd68b27c8d8>

按照规范运行

require 'timer'

describe "Timer" do
  before(:each) do
    @timer = Timer.new
  end

 it "should initialize to 0 seconds" do
    @timer.seconds.should == 0
 end

describe 'time_string' do
 it "should display 0 seconds as 00:00:00" do
   @timer.seconds = 0
   @timer.time_string.should == "00:00:00"
end

it "should display 12 seconds as 00:00:12" do
  @timer.seconds = 12
  @timer.time_string.should == "00:00:12"
end

我的代码:

class Timer

  def timer=(timer  )
    @timer = timer
  end

  def seconds (x = 0)
    @timer = x
  end

  def time_string
   Time.at(@timer).utc.strftime("%H:%M:%S")
  end
 end

我正在进行初始化为0的第一个测试,但我认为我没有做到这一点。

此外,当我在终端ruby中的time_string方法中运行代码时,它返回正确的时间。但我只是不知道如何在@ timer.seconds = 0或...... seconds = 12等时将其发回。

提前致谢!

0 个答案:

没有答案