0

私は 2 番目のギアで立ち往生しています。私のコードを確認し、いくつかの情報を提供してください。ありがとうございました。

class Timer
    def initialize(seconds = 0,time_string = "00:00:00")
        @seconds = seconds
        @time_string = time_string

    end

    def seconds=(new_sec)
        @seconds = new_sec
    end

    def seconds
        @seconds
    end

    def time_string=(new_time)

        hh = seconds/3600
        mm = seconds%3600/60
        ss = seconds%60
        new_time = "#{hh}:#{mm}:#{ss}" 
        @time_string = new_time 
    end

    def time_string
        @time_string
    end
end

Rspec:

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

  it "should display 66 seconds as 00:01:06" do
    @timer.seconds = 66
    @timer.time_string.should == "00:01:06"
  end

  it "should display 4000 seconds as 01:06:40" do
    @timer.seconds = 4000
    @timer.time_string.should == "01:06:40"
  end
end
4

1 に答える 1

0

以下は割り当てdef time_string=(new_time)ですが、実際には new_time を使用して何かの値を変更するわけではないdef time_stringため、getter のみを定義する方が適切です。(あなたのテストは、提供された time_string を介して時間を設定する機能が必要であることを示していません。)

Neil Slater が指摘しているように、インスタンス変数 @time_string は必要なく、time_string メソッドから new_time を返すだけで、必要なものが得られます。だからあなたのコード...

def time_string=(new_time)

        hh = seconds/3600
        mm = seconds%3600/60
        ss = seconds%60
        new_time = "#{hh}:#{mm}:#{ss}" 
        @time_string = new_time 
    end

と置換する

def time_string
  hh = @seconds/3600
  mm = @seconds%3600/60
  ss = @seconds%60
  new_time = "#{hh}:#{mm}:#{ss}" 
end
于 2013-07-07T12:03:21.033 に答える