Ruby : Array : Supports Set algebra (Math) ; Amazing :):

Let U (the universal set)  = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}   (a subset of the positive integers)

        A = {2, 4, 6, 8}
B = {1, 2, 3, 4, 5}
     Union – ALL elements in BOTH sets
                  Intersection – elements where sets overlap
    

Now, in Ruby

pry(main)> u = (1..10).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
pry(main)> a = [2, 4, 6, 8]
=> [2, 4, 6, 8]
pry(main)> b = [1, 2, 3, 4, 5]
=> [1, 2, 3, 4, 5]
# Intersection a n b
pry(main)> a & b
=> [2, 4]
# Union a u b
pry(main)> a | b
=> [2, 4, 6, 8, 1, 3, 5] 
# Append two sets
pry(main)> a + b
=> [2, 4, 6, 8, 1, 2, 3, 4, 5]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s