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

Aimee Knight
Aimee Knight
9,701 Points

Ruby - calling blocks via yield

I noticed in the Ruby videos that Jason tends to prepend method arguments with the ampersand when calling the block via call and yield. However, with some of the simple examples I've run, the method runs with yield when omitting the prepended ampersand in the method arguments. (Initially I read that performance wise it's best to use yield without the prepended ampersand argument.) I see Jason using the prepended ampersand argument while also using yield so, are there other difference between the two approaches that influence Jason's preference in the videos?

2 Answers

Jason Seifer
STAFF
Jason Seifer
Treehouse Guest Teacher

The ampersand explicitly assigns the block to a variable inside of the method. In a lot of circumstances it's a preference thing but there are also times where the behavior is really necessary. For an in depth explanation, check out this article:

http://kswope.com/2011/04/21/tldr-ruby-the-proc-pretzel-operators/