ruby - When to use blocks -


I love Ruby Blocks! The idea behind them is very clean and convenient.

I have just seen my code back in the last week or again, which is basically I have ever written in every Ruby function , and I have noticed that among them Nobody gives a price! Instead of returning values, I always use a block to send back the data!

I have noticed myself writing a small status class which allows me to write code:

  some.v_stuffing. Position | Status.success # Successful code end position. The file # puts the failed code in position. End of terrorism end  

Am I using too many blocks? Is it time to use the blocks and have time to use the return value?

Do you have any information? Will the blocks be of great use and will I ever cut off?

The whole thing will be more readable as:

  . Do_stuff #Unforced code is the second #Unffective code end  

or to use a common rail idiom:

  If the user.save render: Action = >: User. Errors.Each | ARR, msg | Code blocks are overuse when IOMHO, avoid the return of the Boolean value. Login-In. In "# {attr} - # {msg}"} Render: Action =>: Edit End  

A block is understandable if . .

This code allows the resource to be used without closing the resource

  open ("fname"). F | There is no need to worry about closing the file # with the file # #  

The calling code must be calculated with non-trivial result

this In the situation, you avoid adding return values ​​to the calling area. This is also understandable with many values ​​at times.

  Something to do. Res1, res2 | If res1.foo? And res2.bar? Foo (res1) elsef res2.bar? Bar (Ridge 2) End End # has given no reset to the code  

in the scope to call

You can see some Rail Helpliers :

  & lt% content_tag: div do%> & lt% = content_tag: duration "span content"%> & lt% end -%>  

And of course iterator are a great use case, because they are more beautiful than for loops or <(considered as ruby-ists).

Definitely not a complete list, but I suggest that you can not use only blocks because you can.


Comments

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -