Difference between .. and ... in Ruby
In ruby 1...5
gives you a range which doesn't include 5
whereas 1..5
gives you a range which does include 5
eg:
>> (1..5).to_a
[
[0] 1,
[1] 2,
[2] 3,
[3] 4,
[4] 5
]
>> (1...5).to_a
[
[0] 1,
[1] 2,
[2] 3,
[3] 4
]
Difference between and and && in Ruby?
and
is the same as &&
but with lower precedence. They both use short-circuit evaluation.
WARNING: and
even has lower precedence than =
so you'll usually want to avoid and
. An example when and
should be used can be found in the Rails Guide under "Avoiding Double Render Errors".
What is the difference between += and =+ in Ruby?
a += b
is syntactic shorthand fora = a + b
a =+ b
is similar toa = + b
So, firstValue = firstValue + secondValue
is the same as firstValue = firstValue.+(secondValue)
in ruby.firstValue + = secondValue
– increment Add the value of secondValue
to the value of firstValue
, store the result in firstValue
, and return the new value.
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 is the difference between @@ and @ in Ruby?
A variable prefixed with @@
is a class variable and one prefixed with @
is an instance variable. A great description can be found in this answer: https://stackoverflow.com/a/5890199/1181886
Difference between or and || in Ruby?
It's a matter of operator precedence.
||
has a higher precedence than or
.
So, in between the two you have other operators including ternary (? :
) and assignment (=
) so which one you choose can affect the outcome of statements.
Here's a ruby operator precedence table.
See this question for another example using and
/&&
.
Also, be aware of some nasty things that could happen:
a = false || true #=> true
a #=> true
a = false or true #=> true
a #=> false
Both of the previous two statements evaluate to true
, but the second sets a
to false
since =
precedence is lower than ||
but higher than or
.
What's the difference between or and | in ruby?
The |
operator is a binary mathematical operator, that is it does a binary OR and works on a numerical level:
1 | 2
# => 3
4 | 3
# => 7
1 | 2 | 3
# => 3
This is because it's manipulating individual values as if they were binary:
0b01 | 0b10
# => 3 (0b11)
The ||
operator is a logical one, that is it returns the first value that's logically true. In Ruby only literal nil
and false
values evaluates as logically false, everything else, including 0
, empty strings and arrays, is true.
So:
1 || 2
# => 1
0 || 1
# => 0
The or
operator works almost exactly the same as ||
except it's at a much lower precedence. That means other operators are evaluated first which can lead to some problems if you're not anticipating this:
a = false || true
# => true
a
# => true
a = false or true
# => true
a
# => false
This is because it's actually interpreted as:
(a = false) or true
This is because =
has a higher precedence when being evaluated.
What is the difference between '&&' and '&' in Ruby
&&
is a boolean and
. It returns the second argument if the first argument is true-ish. Because 0
is true-ish in Ruby, 1
is returned.
&
is a bitwise and
. It compares the bit representation of the values. Because (imaging 8 bit) 00000000
(0
) and 00000001
(1
) have no 1
digits in common, 00000000
(0
) is returned.
What is the difference between %= ... % and % ... % in Ruby on Rails
This is ERB templating markup (one of many templating languages supported by Rails). This markup:
<% ... %>
is used to evaluate a Ruby expression. Nothing is done with the result of that expression, however. By contrast, the markup:
<%= ... %>
does the same thing (runs whatever Ruby code is inside there) but it calls to_s
on the result and replaces the markup with the resulting string.
In short:
<% just run code %>
<%= run code and output the result %>
For example:
<% unless @items.empty? %>
<ul>
<% @items.each do |item| %>
<li><%= item.name %></li>
<% end %>
</ul>
<% end %>
In contrast, here's some Haml markup equivalent to the above:
- unless @items.empty?
%ul
- @items.each do |item|
%li= item.name
What is the difference between :to and = in rails
In context of Rails routes:
- Is there a difference between these two statements?
There is no difference.
- If so why is one better than the other?
No, it's the same.
- Why is the rails community switching to the ":" notation (or are
they)?
Just a more readable, 'from' => 'to'
and 'from', to: 'to'
- Moving forward with rails 4 and soon 5, are both formats still
acceptable?
Yes.
The =>
notation it's a hash ruby feature, and related to the :symbol
.
You can write symbols by two ways :key => value
and key: value
.
Related Topics
Why Is "Slurping" a File Not a Good Practice
How to Download a Binary File Over Http
Difference Between Collection Route and Member Route in Ruby on Rails
Ruby MySQL2 Gem Installation on Windows 7
Is There a Clean Way to Avoid Calling a Method on Nil in a Nested Params Hash
Understanding Ruby'S Load Paths
How to Override a Setter Method in Ruby on Rails
Is the Ruby Operator ||= Intelligent
How to Pick Randomly from an Array
Is There a "Do ... While" Loop in Ruby
Rails: Access to Current_User from Within a Model in Ruby on Rails