"James Toomey" <jamesvtoomey / yahoo.com> schrieb im Newsbeitrag
news:8fd3ee12.0304161737.32de03da / posting.google.com...
> Can anyone help reduce my confusion? I think I understand the "how" of
> proc blocks, but I still don't get the "why," particularly with a
> dynamically passed block of code, like in the following sample:
>
> def dummyMethod(&dynamicBlock)
>  dynamicBlock.call
> end
>
> dummyMethod {
> puts 'This is line 1 of custom block code'
> puts 'This is line 2 of custom block code'
> }
>
> From what I've read, the proc block feature isn't found in many
> languages, and makes Ruby (and others like Lisp) much more flexible
> and powerful. However, I don't quite grasp why. Is the benefit that
> you don't need to modify "dummyMethod" at all, but rather just modify
> the code within the block?

Exactly.

> And if this is the case, why is that
> better? After all, if you have to make code changes, you still have to
> change it somewhere, regardless of whether you're changing it in
> "dummyMethod" or in the code block. I don't see the benefit of passing
> blocks of code around instead of just including that code in the
> method you're calling.

The benfit is increased modularity.  For example, the ubiquituous method
"each" which typically is used to iterate through some kind of collection
deals with the iteration logic (the collection can be an array, a hash, a
set a tree - whatever) while blocks cover the treatment of iterated
elements.  Without this separation you would

a) have to write a lot of methods containing the iteration and doing
different things to the elements

b) have to introduce a new method for new element treatment which is
especially awful if you're not the creator of the class at hand (i.e.
you're using a module from someone else)

> I'm coming at this from a Visual Basic background, so I'm sure I'm a
> bit like a Neanderthal being shown a power drill and wondering why
> it's any better than my rock tool. I get the feeling that proc blocks
> can do great things, but I just don't see why. And is there any
> benefit to passing a dynamic block as shown above as opposed to
> creating a new Proc object as shown here?

Proc objects can be stored and used later.  Choose which ever is better
suited under certain conditions.

> Thanks for everyone's help! It's very exciting being part of this
> community, which seems to be on the cutting edge of the programming
> future.

:-)  Don't let yourself be swept away too easily.  Ruby is indeed great,
but there are other tools more approriate under certain circumstances.

Regards

    robert