Here is a blog post by Yehuda Katz about what sets Ruby blocks apart:

http://yehudakatz.com/2010/02/07/the-building-blocks-of-ruby/

That should give you a few great examples.

hth
sylvester

On Nov 24, 2011, at 5:01 PM, Jes=FAs Gabriel y Gal=E1n wrote:

> On Thu, Nov 24, 2011 at 4:55 PM, Fily Salas <fs_tigre / hotmail.com> =
wrote:
>> Hi,
>>=20
>> I have been practicing Ruby and believe it or not the hardest part =
for
>> me to understand (so far) is the concept of using blocks, why used =
them
>> and how to use them.
>=20
> There are several benefits and use cases for using blocks.
>=20
>> For instance, I know I don't fully understand blocks but why would =
you
>> use a block in the following situation.
>>=20
>> text =3D "Some Text"
>> File.open('test_file.txt', 'a+') do |file|
>>   file.write(text)
>> end
>>=20
>> Why not just ...?
>>=20
>> text =3D "Some Text"
>>  file =3D File.open('test_file1.txt', 'a+')
>> file.write(text)
>> ..why use a block instead?
>=20
> This is quite straightforward: in this situation the use case is that
> you want to ensure that something happens before and after the block
> code. In this case, the File#open method opens the file, then calls
> the block and then ensures that the file is closed, even in the face
> of an exception within the block.
>=20
> So, first use case: when you want code executed around code that is
> not under your control.
>=20
> Another use case is when you provide a generic functionality that can
> applied to many situations, but you want to leave the exact detailed
> behavior to the client code. For example, the Enumerable#each method
> provides its clients with the capability of traversing an enumerable
> instance, doing something with each element one at a time. What you do
> with the element is responsibility of the client code, so the each
> method just accepts a block to achieve that.
>=20
> Hope this helps and I hope the concept clicks soon, because it's one
> of the things that make Ruby so wonderful.
>=20
> Jesus.
>=20
>=20
>> 1-What would be a good rule of thumb to know when to use blocks?
>> 2-What would be the easiest way to understand block?
>> 3-Can someone be so kind and explain block a little bit?
>>=20
>> Thanks a lot for all of your help!
>> Learning a lot in this forum. Awesome people!
>>=20
>> --
>> Posted via http://www.ruby-forum.com/.
>>=20
>>=20
>=20