Blocks vs Methods


I get that the

cube = { |x| x ** 3 }

saves the block { |x| x ** 3 } as a Proc called 'cube' and that we can "pass the proc to a method that would otherwise take a block, and we don't have to rewrite the block over and over!".

But why would we not just create a new method called cube and call that instead of a proc? Couldn't we just define a new method that cubes all the numbers in the array instead of using a block to do it?


Well, that's a good question, and not easily answered. I've done some research on it and I'm still puzzled by the true difference between those two things.

Ultimately, it feels like the main difference is that methods are usually used as parts of objects (even if they can be used on their own) while procs are ways to "objectify" bits of code in order to use them according to the "everything is object" philosophy in Ruby.

Feel free to have a look at this (it may give you more questions than answers ^^):

If you find something more precise than this, don't forget to post it here, I'm interested.


I'm still wondering this same question - but here's my follow-up to this discussion:

Are Proc's basically purposed to make it easier to pass certain methods that collect blocks, by eliminating the need to re-type a block?

In other words, some methods HAVE to take blocks of code, and Proc's are a way to keep DRY code?


Essentially yes procs are a way to name blocks that you want to reuse in your code.