About Ruby product operator (&) and sum operator (|)

When I was writing code at work, I saw only one operator, neither && nor &. When I looked it up, there was no article unexpectedly, so I will make a note of it. By the way about the sum operator ...

Product operator (&)

https://docs.ruby-lang.org/ja/latest/method/Array/i/=26.html

As written in the reference, it performs the product product operation of the set. Returns a new array of elements present in both arrays. Same as Array # intersection. If you specify an object other than an array, type conversion will be performed implicitly, but I will study this area again.

multiple_operation.rb


a = [1, 2, 3, 4, 5, 6]
b = [2, 4, 6, 8, 10]
a & b
# => [2, 4, 6]

fruits = ['apple', 'banana', 'grape', 'melon', 'strawberry']
favorite_fruits = ['banana', 'cherry', 'peach', 'strawberry']
fruits & favorite_fruits
# => ['banana', 'strawberry']

a = 10
b = 15
a & b
# => 10

a = 0b1010
b = 0b1111
a & b
# => 10

Product operator (|)

https://docs.ruby-lang.org/ja/latest/method/Array/i/=7c.html

This is also as described in the reference, but the union operation of the set is performed. Returns an array containing the elements contained in either array. It says that duplicate elements will be removed, but since it is a union, is it a mistake?

sum_operation.rb


a = [1, 2, 3, 4, 5, 6]
b = [2, 4, 6, 8, 10]
a | b
# =>  [1, 2, 3, 4, 5, 6, 8, 10]

fruits = ['apple', 'banana', 'grape', 'melon', 'strawberry']
favorite_fruits = ['banana', 'cherry', 'peach', 'strawberry']
fruits | favorite_fruits
# => ["apple", "banana", "grape", "melon", "strawberry", "cherry", "peach"]

a = 10
b = 15
a | b
# => 15

a = 0b1010
b = 0b1111
a | b
# => 15

Finally

I would like to be able to post small stories. I myself changed jobs from the medical field and became an engineer, so if there are people with similar circumstances, I would love to interact with them!

Recommended Posts

About Ruby product operator (&) and sum operator (|)
About the [ruby] operator
About Ruby and object model
About Ruby classes and instances
About Ruby single quotes and double quotes
About object-oriented inheritance and about yield Ruby
About Ruby symbols
About ruby ​​form
About Ruby Hashes
About Ruby arrays
About Ruby inheritance
About ruby block
About Ruby Hashes
About Ruby Symbols
About Ruby variables
Ruby and Gem
About Ruby methods
Ruby sum is a sober and amazing story
About Ruby Kernel Module
[Ruby] I thought about the difference between each_with_index and each.with_index
About Ruby error messages
[Ruby] Operator ":: (colon colon)" [constant]
[Ruby] Classes and instances
Ruby logical operator drill
About Ruby exception handling
Symbols and Destructive Ruby
About Ruby Hashes (continued)
About eval in Ruby
[Ruby] Big Decimal and DECIMAL
About Bean and DI
[ruby] About here documents
About classes and instances
About the difference between classes and instances in Ruby
About the ternary operator
About Ruby if statement
A rough note about Ruby arrays and hash objects
About Ruby instance methods
About redirect and forward
Ruby classes and instances
Ruby inheritance and delegation
About encapsulation and inheritance
Ruby variables and methods
[Ruby] About instance generation
[Technical memo] About the advantages and disadvantages of Ruby
About Serializable and serialVersionUID
Be careful about Ruby method calls and variable references
Thinking about logic Ruby
[Ruby] Questions and verification about the number of method arguments
About the difference between "(double quotation)" and "single quotation" in Ruby
[Ruby] About the difference between 2 dots and 3 dots of range object.
Study Java # 2 (\ mark and operator)
Difference between product and variant
About for statement and if statement
About synchronized and Reentrant Lock
GraphQL Ruby and actual development
Ruby syntax errors and countermeasures
[Ruby on Rails] about has_secure_password
About regular expressions in Ruby
[Java] About String and StringBuilder
About the same and equivalent
Ruby C extension and volatile