Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

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

;)

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. :)