mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 07:08:01 +00:00
Add Or Operator Precedence as a ruby til
This commit is contained in:
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
|
|||||||
warrant a full blog post. These are mostly things I learn by pairing with
|
warrant a full blog post. These are mostly things I learn by pairing with
|
||||||
smart people at [Hashrocket](http://hashrocket.com/).
|
smart people at [Hashrocket](http://hashrocket.com/).
|
||||||
|
|
||||||
_462 TILs and counting..._
|
_463 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -337,6 +337,7 @@ _462 TILs and counting..._
|
|||||||
- [Limit Split](ruby/limit-split.md)
|
- [Limit Split](ruby/limit-split.md)
|
||||||
- [Listing Local Variables](ruby/listing-local-variables.md)
|
- [Listing Local Variables](ruby/listing-local-variables.md)
|
||||||
- [Next And Previous Floats](ruby/next-and-previous-floats.md)
|
- [Next And Previous Floats](ruby/next-and-previous-floats.md)
|
||||||
|
- [Or Operator Precedence](ruby/or-operator-precedence.md)
|
||||||
- [Override The Initial Sequence Value](ruby/override-the-initial-sequence-value.md)
|
- [Override The Initial Sequence Value](ruby/override-the-initial-sequence-value.md)
|
||||||
- [Parallel Bundle Install](ruby/parallel-bundle-install.md)
|
- [Parallel Bundle Install](ruby/parallel-bundle-install.md)
|
||||||
- [Pass A Block To Count](ruby/pass-a-block-to-count.md)
|
- [Pass A Block To Count](ruby/pass-a-block-to-count.md)
|
||||||
|
|||||||
37
ruby/or-operator-precedence.md
Normal file
37
ruby/or-operator-precedence.md
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
# Or Operator Precedence
|
||||||
|
|
||||||
|
What's the difference between `||` and `or` in Ruby?
|
||||||
|
|
||||||
|
Let's look at an example to find out. First, let's start with some boolean
|
||||||
|
variables:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> a, b = false, true
|
||||||
|
=> [false, true]
|
||||||
|
```
|
||||||
|
|
||||||
|
Now, let's try the different _or_ operators:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> a || b
|
||||||
|
=> true
|
||||||
|
> a or b
|
||||||
|
=> true
|
||||||
|
```
|
||||||
|
|
||||||
|
Cool, they seem to work as expected.
|
||||||
|
|
||||||
|
Finally, let's capture the result in a variable:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
> c = a or b
|
||||||
|
=> true
|
||||||
|
> c
|
||||||
|
=> false
|
||||||
|
```
|
||||||
|
|
||||||
|
But why is `c` false and not true? Operator precedence. The assignment
|
||||||
|
operator (`=`) takes precedence over the `or` operator causing `c` to be
|
||||||
|
assigned to the value of `a` (`false`) before `or`'d with `b`.
|
||||||
|
|
||||||
|
[source](http://stackoverflow.com/questions/2083112/difference-between-or-and-in-ruby)
|
||||||
Reference in New Issue
Block a user