Login:
Senha:
Para logar, você precisa ter o Javascript habilitado.
Busca avançada
Autor:
Título:
Conteúdo:
campo=valor
Sete universos nada paralelos

DEUS EXISTE E FUNCIONA!
"Tudo o que com fé pedirdes em oração, recebereis" (Mt 21,22)
Two ways to simplify Array blocks in Ruby
2011-03-29 20:12:56
2011-03-22 11:27:33
Autor: Sony Santos
https://gigawiki.com/sony/two-ways-to-simplify-array-blocks-in-ruby
Permalink: https://gigawiki.com/970
categ = articles, tips; language = English
1. If you have a block of the form { |v| v.mtd }:
ary.each { |v| v.mtd }          # normal way
ary.each(&:mtd)                 # simpler way

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

That works for other Array methods, like mapMore on it.

2. If you have a block of the form { |v| mtd v }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 more on it.

(This same content was reproduced on my Ruby blog.)
blog comments powered by Disqus