Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

237
Visualizações
What does :& argument mean?

I don't understand the :& argument for the inject method in this example:

[1, 2].map do |id|
  my_custom_method(MyModel.find(id))
end).inject(:&).map(&:category).uniq.compact.sort

:+ makes sense to me:

[1, 2, 3].inject(:+)

is the same as:

[1, 2, 3].inject { |sum, number| sum + number }

What does :& mean, as in the example above?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Enumerable#inject

If you specify a symbol instead, then each element in the collection will be passed to the named method of memo

So & is just Integer#& method

Bitwise AND; each bit in the result is 1 if both corresponding bits in self and other are 1, 0 otherwise

[7, 5].inject(:&) # => 5

It's the same as

7 & 5 # => 5

And how it works

7.to_s(2) # => "111"
5.to_s(2) # => "101"
7: 1 1 1
   & & &
5: 1 0 1
--------
5: 1 0 1
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda