Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!