20 Feb '13, 4am

Jay Fields' Thoughts: Ruby: Invoking a method with a ampersand-parameter:

On a few ocassions recently I've been asked what the & does when it is used in the context of a method invocation parameter. For example, consider the following code, specifically line 6 where collect is called.1. # traditional 2. [1,2,3].collect { |number| number % 2 } #=> [1,0,1] 3. 4. # collect with a previously created block 5. block = lambda { |number| number % 2 } 6. [1,2,3].collect &block #=> [1,0,1] It's fairly easy to see what is going on from the above example; however, more often I run into code that looks like the following code.def clone_collect(&block) clones = collect { |item| item.dup } clones.collect &block end The previous example shows a method that clones each item and then passes on the block to the collect method of the clones array. The above code seems to be a tripping point, I assume it's because the block is defined outside the method definition. ...

Full article: http://blog.jayfields.com/2007/01/ruby-invoking-method-wi...

Tweets

Cheap Designer Hermes Birkin Handbags For Sale ...

article356.com 19 Feb '13, 3am

Terry Stearns explained honestly: “I ‘m right now dealing with the biggest concern needs to be to grasp in addition to kno...