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 ...
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
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
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