Ruby Ruby Blocks Working With Blocks Block Method Practice: Custom Classes

ellie adam
ellie adam
23,606 Points

Challenge Task 2 of 2 Call the run method on the benchmarker

Call the run method on the benchmarker object and pass it two arguments. The first argument should be a string of the description. The second argument can be a block with any code you choose.

class SimpleBenchmarker
  def run(description, &block)
    start_time = Time.now
    block.call
    end_time = Time.now
    elapsed = end_time - start_time

    puts "\n#{description} results"
    puts "Elapsed time: #{elapsed} seconds"
  end
end

benchmarker = SimpleBenchmarker.new
def run(description, &block)
  benchmarker.run "sleep a random amount of time" do
    5.times do 
      print"."
      sleep(rand(0.1..1.0))
end

end

my error is The run method was not called. [:methods_called, :methods_called]

simple_benchmarker.rb
class SimpleBenchmarker
  def run(description, &block)
    start_time = Time.now
    block.call
    end_time = Time.now
    elapsed = end_time - start_time

    puts "\n#{description} results"
    puts "Elapsed time: #{elapsed} seconds"
  end

end
benchmarker = SimpleBenchmarker.new
def run(description, &block)
  benchmarker.run "whatever", do
end
end

2 Answers

Hi Ellie,

Look almost perfect, you should remove definition run method with end in the middle. Also, remove coma after description "whatever".

The reason is that we use run method to call benchmarker object in line 3, we don't have to create new another run method in definition in line 2.

For example:

benchmarker = SimpleBenchmarker.new

def run(description, &block)

benchmarker.run "whatever", do

end

end

;)

ellie adam
ellie adam
23,606 Points

thanks Salman! It did't let me pass challenge one until I added def second time. Then I made challenge two complicated. thanks for help :)

Awesome, please make sure to mark good answers on this question and any others you post to previous questions, it would be greatly appreciate. :)