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

212
Visualizações
Interaction of attr_accessor and |= operator

I understand that x |= y basically means x = x|y. The operator | is defined for the class Set as calculating the union of two sets. Indeed we can see:

require 'set'
r = Set.new(%w(a b)); 
s = Set.new(%w(c d)); 
r |= s; # r => #<Set: {"a", "b", "c", "d"}>

But see for instance the following class definition:

class Demo; 
  def initialize(s); 
    @x = s.dup; 
  end; 
  attr_accessor :x; 
  def m!(t); 
    x |= t.x; ' <--- HERE TROUBLE!
 end; 

end

I'm using this class like this:

u = Demo.new(Set.new(%w(a b)))
v = Demo.new(Set.new(%w(c d)))
u.m!(v)

If I now look at u.x, I see that it still holds the set a,b and not a,b,c,d. My feeling is that this comes from the fact I'm using the attribut accessor, in particular the setter method. If I would write @x |= t.x, it would work. Could it be that the left hand side of x |= t.x uses the getter x and not the setter x=, thereby creating the union in a temporary Set object?

BTW, I'm using a fairly old version of Ruby (JRuby 1.7.x, corresponding roughly to the language version of Ruby 1.9.3).

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

0

The reason for this is that in

x = 3

x is always interpreted as a local variable before ruby starts looking for a method x=. This means, that your method translates to:

def m!(t)
  x = nil # local variable initialization
  x = x | t.x
end;

To solve this, you need to use explicit self to force method call:

def m!(t); 
  self.x |= t.x
end

Another note - please do not use semicolons in ruby. There are only a very rare situations they needed but we generally avoid them

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