zallarak
zallarak

Reputation: 5515

Understanding ruby blocks

What is a good conceptual way to look at Ruby blocks?

if I have an Array, A, and I pass it to the following function:

def MergeSort(&var)
...
end

And then run the function, passing an Array into it:

MergeSort(A)

Will the array A be treated like a block in the function? What is the benefit of blocks, and what are good patterns to use them in? I realize these are kind of unrelated and potentially random questions; any answer that clarifies Ruby blocks would be appreciated.

Upvotes: 3

Views: 1600

Answers (2)

Mischa
Mischa

Reputation: 43298

Understanding Ruby Blocks, Procs and Lambdas by Robert Sosinski explains it very thoroughly.

Upvotes: 8

carpamon
carpamon

Reputation: 6623

You may want to check this article http://blog.codahale.com/2005/11/24/a-ruby-howto-writing-a-method-that-uses-code-blocks/. I recommend you to read it carefully to fully understand blocks in ruby.

Upvotes: 1

Related Questions