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]

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 

Want me to help develop your next project or application?

Contact Us

Specializing in Ruby-on-Rails, Javascript, Multi-Platform Electron Desktop Apps and Hybrid Cordova Mobile Apps.

Recommended Posts