Método inject

48 views
Skip to first unread message

Lean

unread,
Feb 28, 2013, 12:58:34 PM2/28/13
to rail...@googlegroups.com

  Pessoal, 

  Não tenho tanta experiência e gostaria de saber se alguém pode me explicar de uma maneira mais fácil o que faz o método ruby inject...

  Não to conseguindo entender direito...


  Valews!

Alex Takitani

unread,
Feb 28, 2013, 1:09:41 PM2/28/13
to rail...@googlegroups.com


2013/2/28 Lean <lean.fi...@gmail.com>
--
--
Você recebeu essa mensagem porquê está inscrito no Google
Groups "rails-br".
Para enviar uma mensagem para o grupo, mande um email para rail...@googlegroups.com
Para se descadastrar, mande um e-mail para
rails-br+u...@googlegroups.com
Visite o grupo em http://groups.google.com/group/rails-br?hl=pt-BR
Leia nossa política de uso: http://goo.gl/YGgt7
 
---
Você está recebendo esta mensagem porque se inscreveu no grupo "rails-br" dos Grupos do Google.
Para cancelar a inscrição neste grupo e parar de receber seus e-mails, envie um e-mail para rails-br+u...@googlegroups.com.
Para obter mais opções, acesse https://groups.google.com/groups/opt_out.
 
 

Celso Dantas

unread,
Feb 28, 2013, 1:10:01 PM2/28/13
to rail...@googlegroups.com
http://railspikes.com/2008/8/11/understanding-map-and-reduce

tenta ler isso daí.
é um algoritmo padrão pra "soma" entre elementos de uma lista.


2013/2/28 Lean <lean.fi...@gmail.com>
--

Lean Figueredo

unread,
Feb 28, 2013, 1:22:10 PM2/28/13
to rail...@googlegroups.com
  Celso,

  Valeu mesmo, comecei a clarear....

  Obrigado pela atençaõ de todos!

  Leandro Figueredo

Cezinha

unread,
Mar 1, 2013, 7:49:41 AM3/1/13
to rail...@googlegroups.com
Ele serve para você fazer soma de valores em Arrays, por exemplo.


valores = [10, 20, 30, 40, 50]

você poderia fazer algo assim


soma = 0
valores.each { |valor| soma += valor }

MAS naõ é legal, ao invés você poderia fazer

soma = valores.inject { |soma, valor| soma += valor }

ou

soma = valores.inject(50) { |soma, valor| soma += valor } nesse caso já viria com um valor padrão...


 

Alex Takitani

unread,
Mar 1, 2013, 8:33:19 AM3/1/13
to rail...@googlegroups.com
Não faz só soma

Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.

In mathematics, a binary operation on a set is a calculation involving two elements of the set (called operands) and producing another element of the set (more formally, anoperation whose arity is two). Examples include the familiar elementary arithmetic operations of additionsubtractionmultiplication and division. Other examples are readily found in different areas of mathematics, for example, vector additionmatrix multiplication and conjugation in groups.

(1..4).inject(:+)
 => 10 
(1..4).inject(:*)
 => 24
(1..4).inject(:-)
 => -8 

# find the longest word
longest = %w{ cat sheep bear }.inject do |memo,word|
   memo.length > word.length ? memo : word
end
longest                                       #=> "sheep"





2013/3/1 Cezinha <lcce...@gmail.com>



 

--

Ivan Joaquim (Pichot)

unread,
Mar 1, 2013, 8:43:06 AM3/1/13
to rail...@googlegroups.com
No link que o Celso enviou ( http://railspikes.com/2008/8/11/understanding-map-and-reduce ) , está dizendo que é melhor usar o 'reduce' ao 'inject'.

Legal a thread, não conhecia nenhum desses métodos, era tudo na base do '.each'.

O que acham? exites diferenças entre os dois?


2013/3/1 Alex Takitani <atp...@gmail.com>

Alex Takitani

unread,
Mar 1, 2013, 8:47:32 AM3/1/13
to rail...@googlegroups.com
reduce e inject são o mesmo método


2013/3/1 Ivan Joaquim (Pichot) <pich...@gmail.com>

Celso Dantas

unread,
Mar 1, 2013, 9:22:13 AM3/1/13
to rail...@googlegroups.com
O reduce so tem um nome mais fácil de lembrar e entender. Ja q o método reduz uma lista de objetos a um único elemento. Por isso q n é uma soma. 


On Friday, March 1, 2013, Alex Takitani wrote:
reduce e inject são o mesmo método


2013/3/1 Ivan Joaquim (Pichot) <pich...@gmail.com>
No link que o Celso enviou ( http://railspikes.com/2008/8/11/understanding-map-and-reduce ) , está dizendo que é melhor usar o 'reduce' ao 'inject'.

Legal a thread, não conhecia nenhum desses métodos, era tudo na base do '.each'.

O que acham? exites diferenças entre os dois?


2013/3/1 Alex Takitani <atp...@gmail.com>
Não faz só soma

Combines all elements of enum by applying a binary operation, specified by a block or a symbol that names a method or operator.

In mathematics, a binary operation on a set is a calculation involving two elements of the set (called operands) and producing another element of the set (more formally, anoperation whose arity is two). Examples include the familiar elementary arithmetic operations of additionsubtractionmultiplication and division. Other examples are readily found in different areas of mathematics, for example, vector addition

Everaldo Gomes

unread,
Mar 1, 2013, 10:18:20 AM3/1/13
to rail...@googlegroups.com
collect -> map
inject -> reduce


2013/3/1 Celso Dantas <celso...@gmail.com>
Reply all
Reply to author
Forward
0 new messages