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
darius goore
3,803 PointsBlocks benchmarking example
I don't understand the following:
"The times method will simply repeat however many times," I understand that "a" corresponds to the number of times the loop runs in the block and that i can add a print a in the superloop to see that. I don't understand how the "how_many variable gets defined Also, in this example we use block.call instead of yield. why?
4 Answers
minzawmra
Courses Plus Student 15,682 Points'how_many' is set by default to 1 if nothing is passed. It is a local variable of the method 'go.' In the example, it is set to 5 when called through: SimpleBenchmarker.go 5 <block>
darius goore
3,803 PointsThanks! Why do we not use yield here?
minzawmra
Courses Plus Student 15,682 PointsYou can also use yield. I think it's just a matter of choice. block.call reads better and looks more obvious than yield.
Elaine Ryan
6,771 PointsThanks! So for
how_many.times do |a|
is "a" getting passed 5?
Are we calling some public method called times on an object called how_many?
And in the self.go method which is the block that &block refers to?
I'm pretty confused here.