Rails

Basic Operations For Arrays In Ruby

Posted by Weston Ganger on November 29, 2016

After developing for a long time I wanted a way to get the uniq values from two arrays combined. Turns out there is a very simple union operation for this exact problem.

Here I will show four basic operations for Arrays:


x = [1,2,3]
y = [2,3,4,5]

#intersection
x & y # => [2,3]

# union
x | y # => [1,2,3,4,5]

# difference
x - y # => [1]

# combination
x + y # => [1,2,3,2,3,4,5]


Related External Links:


Posted in Rails and Tagged with array ruby 


Need help on your next project or application?

I specialize in Ruby-on-Rails, AngularJS, Javascript, Bootstrap, and Hybrid Mobile Apps with Cordova & Ionic.

Contact Me

Recommended Posts