2014-05-20 15 views
0

我不能理解我在這個練習中被問到的是什麼(我必須得到以下測試才能通過):性能監控rspec測試:「需要大約0秒來運行一個空的塊」做

require "performance_monitor" 

require "time" # loads up the Time.parse method -- do NOT create time.rb! 

describe "Performance Monitor" do 
    before do 
    @eleven_am = Time.parse("2011-1-2 11:00:00") 
    end 

    it "takes about 0 seconds to run an empty block" do 
    elapsed_time = measure do 
    end 
    elapsed_time.should be_within(0.1).of(0) 
    end 

我嘗試創建一個空方法,但它不會通過。

def measure(number = 1) 
    number 
end 

def measure 
end 

我覺得我不理解什麼測試要求我建立。它在問什麼,我怎樣才能更好地解釋它。

回答

0

measure應接受一個塊,執行它的時間並返回該塊運行所花費的秒數。這裏是僞代碼(它可以很直接地轉換成你需要的ruby代碼)

def measure(&block) 
    time t1 = current time 
    execute block 
    return (current time - t1) # difference between current time *now* and 
          # time that was current before execution of the block 
          # which (the difference) means time the block took to run 
end