<?xml version='1.0' encoding='UTF-8'?>
<elementos_gw>
  <documento>
    <id>970</id>
    <autor>5</autor>
    <nome>Two ways to simplify Array blocks in Ruby</nome>
    <nome_facil>two ways to simplify array blocks in ruby</nome_facil>
    <criacao>2011-03-22 11:27:33</criacao>
    <alteracao>2011-03-29 20:12:56</alteracao>
    <texto>1. If you have a block of the form {{{&amp;#123; |v| v.mtd &amp;#125;}}}:
{{{
ary.each { |v| v.mtd }          # normal way
ary.each(&amp;:mtd)                 # simpler way

# Example:
ary = %w(abc def ghi jkl)
ary.each(&amp;:upcase!)             # same as ary.each { |v| v.upcase! }
}}}

That works for other Array methods, like {{{map}}}. [http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Method_Calls#The_ampersand_.28.26.29 More on it.]

2. If you have a block of the form {{{&amp;#123; |v| mtd v &amp;#125;}}}, //and// the method {{{mtd}}} does the same thing with all parameters:
{{{
ary.each { |v| mtd v }          # normal way
mtd *ary                        # simpler way

# Example:
ary = %w(abc def ghi jkl)
puts *ary                       # same as ary.each { |v| puts v }
}}}

See [http://theplana.wordpress.com/2007/03/03/ruby-idioms-the-splat-operator/ more on it.]

(This same content was reproduced on [http://rubychallenger.blogspot.com/2011/03/two-ways-to-simplify-array-blocks-in.html my Ruby blog].)</texto>
    <publico>1</publico>
    <original>0</original>
    <anterior>0</anterior>
    <versao>0</versao>
    <traducao>0</traducao>
    <propriedade>
      <nome>language</nome>
      <valor>English</valor>
      <publico>1</publico>
    </propriedade>
    <propriedade>
      <nome>categ</nome>
      <valor>tips</valor>
      <publico>1</publico>
    </propriedade>
    <propriedade>
      <nome>categ</nome>
      <valor>articles</valor>
      <publico>1</publico>
    </propriedade>
  </documento>
</elementos_gw>
