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 trialSophie Rae
3,066 Pointssimple benchmarker question
Hi!
So I understand all the code in the simple benchmarker program apart from this bit:
benchmarker = SimpleBenchmarker.new benchmarker.run
What is the purpose of this in the code/what does it do?
Thanks :)
1 Answer
Kourosh Raeen
23,733 PointsIf I'm correct your question is about this Code Challenge: https://teamtreehouse.com/library/ruby-blocks/working-with-blocks/block-method-practice-custom-classes
The first line of the code you posted creates an instance of the SimpleBenchmarker class:
benchmarker = SimpleBenchmarker.new
Then in task 2 of the challenge you are asked to call the run method on that instance. The run method needs two arguments, a description and a block. The description should be a string and the block can be any block of code:
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
benchmarker.run "Printing numbers from 1 to 100" do
for number in (1..100) do
puts "Now printing number {number}"
end
end