Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

ellie adam
ellie adam
26,376 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
26,376 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. :)