Set operations on ruby arrays

Nikolaus Gradwohl2009-11-28T05:23:00+01:00

Ruby provides some very interesting set operations on arrays.

given the two arrays A and B wich look like this

A = ["A", "B", "C", "D"]
B = ["C", "D", "E"]

There are three set operations we can use that union, intersection and difference.

Union

union

A | B

contains all elements from both sets without doublicates. So this results in ["A", "B", "C", "D", "E"]

Difference

difference

A - B

contains all elements from set A that are not in set B. So this results in ["A", "B"]

Intersection

intersection

A & B

contains all elements that are in set A and in set B. So this results in ["C", "D"]

Tweet This! submit to reddit Digg!   Tags: | 1 comments | no trackbacks

See also:

Calculating Euclidean Rhythmns using the Bresenham Algorithm
ruby rhyme generator
Curling Animation II
Blinking Logfile Map
Curling Recursion

Trackbacks

Comments

Leave a response

  1. hoppinjohnz 2012-10-01T20:41:52+02:00

    Thanks for the posting. This is what I was looking for.

Leave a comment