What Does the "===" Operator Do in Ruby

What does -@ operator do in Ruby?

-@ and +@ are simply the method names for unary - and +. If you want to redefine them, invoke them as methods, etc., that's how you need to refer to them to distinguish them from binary - and +.

What does the === operator do in Ruby?

Just like with every other method in Ruby (or actually pretty much any object-oriented language),

a === b

means whatever the author of a's class wants it to mean.

However, if you don't want to confuse the heck out of your colleagues, the convention is that === is the case subsumption operator. Basically, it's a boolean operator which asks the question "If I have a drawer labelled a would it make sense to put b in that drawer?"

An alternative formulation is "If a described a set, would b be a member of that set?"

For example:

 (1..5) === 3           # => true
(1..5) === 6 # => false

Integer === 42 # => true
Integer === 'fourtytwo' # => false

/ell/ === 'Hello' # => true
/ell/ === 'Foobar' # => false

The main usage for the === operator is in case expressions, since

case foo
when bar
baz
when quux
flurb
else
blarf
end

gets translated to something (roughly) like

_temp = foo

if bar === _temp
baz
elsif quux === _temp
flurb
else
blarf
end

Note that if you want to search for this operator, it is usually called the triple equals operator or threequals operator or case equality operator. I really dislike those names, because this operator has absolutely nothing whatsoever to do with equality.

In particular, one would expect equality to be symmetric: if a is equal to b, then b better be also equal to a. Also, one would expect equality to be transitive: if a == b and b == c, then a == c. While there is no way to actually guarantee that in a single-dispatch language like Ruby, you should at least make an effort to preserve this property (for example, by following the coerce protocol).

However, for === there is no expectation of either symmetry or transitivity. In fact, it is very much by design not symmetric. That's why I don't like calling it anything that even remotely resembles equality. It's also why I think, it should have been called something else like ~~~ or whatever.

Clarification on the Ruby Operator

Ruby is an object-oriented language. The fundamental principle of object orientation is that objects send messages to other objects, and the receiver of the message can respond to the message in whatever way it sees fit. So,

a << b

means whatever a decides it should mean. It's impossible to say what << means without knowing what a is.

As a general convention, << in Ruby means "append", i.e. it appends its argument to its receiver and then returns the receiver. So, for Array it appends the argument to the array, for String it performs string concatenation, for Set it adds the argument to the set, for IO it writes to the file descriptor, and so on.

As a special case, for Fixnum and Bignum, it performs a bitwise left-shift of the twos-complement representation of the Integer. This is mainly because that's what it does in C, and Ruby is influenced by C.

What does the '||=' operator do in ruby?

what does || do? If you have a and b then a || b is true if and only if either a or b is true. It is the same with ||= this operator combines two operations '=' and '||'. So a ||= b is equivelent to c || c = b

EDIT: so in your context ENV['ENVIRONMENT'] ||= 'test' means that if ENV['ENVIRONMENT'] is not nil and not false it will preserve its value, otherwise it will become 'test' and after that the new value of ENV['ENVIRONMENT'] is assigned to RACK_ENV

What does the (unary) * operator do in this Ruby code?

The * is the splat operator.

It expands an Array into a list of arguments, in this case a list of arguments to the Hash.[] method. (To be more precise, it expands any object that responds to to_ary/to_a, or to_a in Ruby 1.9.)

To illustrate, the following two statements are equal:

method arg1, arg2, arg3
method *[arg1, arg2, arg3]

It can also be used in a different context, to catch all remaining method arguments in a method definition. In that case, it does not expand, but combine:

def method2(*args)  # args will hold Array of all arguments
end

Some more detailed information here.

Rails—what does &= do?

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.

What does ||= (or-equals) mean in Ruby?

This question has been discussed so often on the Ruby mailing-lists and Ruby blogs that there are now even threads on the Ruby mailing-list whose only purpose is to collect links to all the other threads on the Ruby mailing-list that discuss this issue.

Here's one: The definitive list of ||= (OR Equal) threads and pages

If you really want to know what is going on, take a look at Section 11.4.2.3 "Abbreviated assignments" of the Ruby Language Draft Specification.

As a first approximation,

a ||= b

is equivalent to

a || a = b

and not equivalent to

a = a || b

However, that is only a first approximation, especially if a is undefined. The semantics also differ depending on whether it is a simple variable assignment, a method assignment or an indexing assignment:

a    ||= b
a.c ||= b
a[c] ||= b

are all treated differently.

What does &. (ampersand dot) mean in Ruby?

It is called the Safe Navigation Operator. Introduced in Ruby 2.3.0, it lets you call methods on objects without worrying that the object may be nil(Avoiding an undefined method for nil:NilClass error), similar to the try method in Rails.

So you can write

@person&.spouse&.name

instead of

@person.spouse.name if @person && @person.spouse

From the Docs:

my_object.my_method

This sends the my_method message to my_object. Any
object can be a receiver but depending on the method's visibility
sending a message may raise a NoMethodError.

You may use &. to designate a receiver, then my_method is not invoked
and the result is nil when the receiver is nil. In that case, the
arguments of my_method are not evaluated.

What does the operator ?= mean?

The ?= operator is known as the existential operator in CoffeeScript.

From the docs:

It's a little difficult to check for the existence of a variable in JavaScript. if (variable) ... comes close, but fails for zero, the empty string, and false. CoffeeScript's existential operator ? returns true unless a variable is null or undefined, which makes it analogous to Ruby's nil?

That means that the way it works is, using your example:

 @variable1 ?= @object1.element[0]

If @variable1 is null or undefined, assign @object1.element[0] to it.

what if I want to add the condition for "else" ?

@variable1 = 
if @variable1?
@object1.element[0]
else
# your else clause here


Related Topics



Leave a reply



Submit