Hi, i’ve recently started learning Ruby i spent a lot of time reading
and i’ve started working on Test-First Teaching exercise to test what i
have learned. But i got stuck on the performance monitor problem. I cant
seem to understand exactly what it’s asking me to do.
First it asked me to
describe “Performance Monitor” do
before do @eleven_am = Time.parse(“2011-1-2 11:00:00”)
end
Am i supposed to create a class Performance_monitor and creating an
object variable eleven_am ?
but when i called rake i cant even get pass the “takes about 0 seconds
to run an empty block”. I’ve attempted something simple to test an empty
block but got no result.
def measure(&block)
yield
end
some help on exactly what i’m supposed to be doing would be greatly
appreciated.
The trick – and it is a trick – is to create a method (not a class)
that calls a block and returns the amount of time that block took to
run. That’s it. All the stuff in the test (including eleven_am) is
there to properly test that that simple method works right. It’s hard
because it’s easy
I got this error
takes about 1 second to run a block that sleeps for 1 second (FAILED -
1)
expected 0.0 to be within 0.1 of 1
What this tells you is that the test was expecting the result to be
between 0.9 and 1.1, and it’s actually 0.0.
As the block is sleeping for one second, it seems that your
calculation of the time is wrong.
Take a look at the method you are using:
I got this error
takes about 1 second to run a block that sleeps for 1 second (FAILED -
1)
Failures:
Performance Monitor takes about 1 second to run a block that sleeps
for 1 second
Failure/Error: elapsed_time.should be_within(0.1).of(1)
expected 0.0 to be within 0.1 of 1
oh wait i’m stupid i wrote the math wrong it should be
end_time - start_time
oops! anyway with that out of the way all i had to do was find the
average time for the last part
which is divide the current time by amount entered. just in case anyone
needs help with this in the future the answer is below
require “time”
def measure(repeat = 1)
start_time = Time.now
repeat.times { |x|
yield x
}
end_time = Time.now
elapsed_time = (end_time - start_time)/repeat
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.