What does the % operator do in Ruby in N % 2?
%
is the modulo operator. The result of counter % 2
is the remainder of counter / 2
.
n % 2
is often a good way of determining if a number n
is even or odd. If n % 2 == 0
, the number is even (because no remainder means that the number is evenly divisible by 2); if n % 2 == 1
, the number is odd.
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.
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 +
.
Ruby ** double star operator
** is Exponent Operator- It performs exponential (power) calculation. Let me explain by this simple example
2 ** 2 => 2 * 2 => 4
2 ** 3 => 2 * 2 * 2 => 8
2 ** 4 => 2 * 2 * 2 * 2 => 16
2 ** 5 => 2 * 2 * 2 * 2 * 2 => 32
so 43 ** 67 => 43 * 43 * 43 * 43 ...............................................................
so it results in such a big number.
To get more details on operatorts http://www.tutorialspoint.com/ruby/ruby_operators.htm
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 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
How does the :: operator work in Ruby?
If you remove the last line, you will see that you will get 5, 4, 3, 2
. The reason is that the body of classes and modules is just regular code (unlike in some other languages). Therefore, those print statements will be executed when the classes/modules are getting parsed.
As to how ::
works - it just lets you move around the scopes. ::A
will reference the A
in the main scope. Just A
will refer to A
in the current scope. A::B
will refer to the B
, that is inside the A
, that is inside the current scope.
what is the difference between += and =+ in ruby?
There's no such token as =+
; it's actually two tokens: assignment followed by the unary +
operator; the latter is essentially a no-op, so @@num_things =+ 1
is equivalent to @@num_things = 1
.
Since there is a +=
token, the language parser will parse it as a single token.
(In the early formulations of BCPL which was the precursor to C, the modern -=
operator was written as =-
.)
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.
What is stand for in ruby with integer
As the documentation says, Integer:<< - Returns the integer shifted left "X" positions, or right if "X" is negative. In your scenario is shifts 8 positions to the left.
Here is how it works:
8.to(2) => "1000"
Now let's shift "1000"
8 positions to the left
(8 << 8).to_s(2) => "100000000000"
If you count the 0
above you will see it added 8
after "1000"
.
Now, let's see how it returns 2048
"100000000000".to_i(2) => 2048
Related Topics
Rails: How to Determine Controller/Action in View
All Possible Permutations of a Given String
How to Delete All Contents of a Folder with Ruby-Rails
Possible to Alias a Belongs_To Association in Rails
How to Call Applicationcontroller Methods from Applicationhelper
Multiple Robots.Txt for Subdomains in Rails
Augmenting a Model from an External Gem
Collect Values from an Array of Hashes
Creating Categories on Jekyll Driven Site
How to Read the Body Text of an Email Using Ruby's Net/Imap Library
Ruby Classes: Initialize Self VS. @Variable
$Redis Global Variable with Ruby on Rails
How to Override Static Class Method Using Module in Ruby
How to Sort a Hash by Value in Descending Order and Output a Hash in Ruby
How to Use Ruby Minitest::Spec with Rails for API Integration Tests