Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails—what does &= do?

To help future searches, it could also be described as "and equals" or "ampersand equals".

I found this line in the Rails source code:

attribute_names &= self.class.column_names

What is the function of &=?

like image 606
Mirror318 Avatar asked Sep 13 '19 08:09

Mirror318


People also ask

What is the use of Rails?

Rails is a development tool which gives web developers a framework, providing structure for all the code they write. The Rails framework helps developers to build websites and applications, because it abstracts and simplifies common repetitive tasks.

What language does Rails use?

Rails combines the Ruby programming language with HTML, CSS, and JavaScript to create a web application that runs on a web server. Because it runs on a web server, Rails is considered a server-side, or “back end,” web application development platform (the web browser is the “front end”).

What do you mean by Ruby on Rails?

Ruby on Rails, sometimes known as "RoR" or just "Rails," is an open source framework for Web development in Ruby, an object-oriented programming (OOP) language similar to Perl and Python.

Is Rails or Django better?

In the battle of Django vs Ruby on Rails performance, it is observed that Rails is faster by 0.7%. It is because Rails has the advantage of a rich repository of amazing libraries and plugins to enhance the speed and eventually the performance of this framework.


1 Answers

The so-called operator-assignments of the form a &= b, where & can be another binary operator, is (almost but not quite — the boolean operators are a notable exception having some corner cases where they differ) equivalent to a = a & b.

Since operators in Ruby are methods that are called on the left operand, and can be overridden, a good way to know what they are is to look at the documentation for the class of the left operand (or one of their ancestors).

attribute_names you found, given the context, is likely ActiveRecord::AttributeMethods#attribute_names, which

Returns an array of names for the attributes available on this object.

Then we can see Array#&, which performs

Set Intersection — Returns a new array containing unique elements common to the two arrays. The order is preserved from the original array.

It compares elements using their hash and eql? methods for efficiency.

like image 95
Amadan Avatar answered Sep 28 '22 18:09

Amadan