Article

New to me: subtracting arrays

I was looking at a way to find the difference between two arrays and my friend Ben clued me in to a nice shortcut I wasn’t familiar with:

>> fruits = ['apple', 'bannana', 'grapes']

>> fruits_i_hate = ['banana', 'blueberries']

>> fruits_i_like = fruits - fruits_i_hate

=> ['apple', 'grapes']

You can even string them together:

>> fruits - fruits_i_hate - fruits_that_are_bad_for_me

(or something like that).

Pretty cool.

Comments (One comment)

We noticed that your blog have some Ruby On Rails releated content, and that is why we would like to invite you to register yourself and your blog at RubyCorner, a directory of Ruby related blogs: http://www.rubycorner.com/
We like to think about RubyCorner as a “meeting place” for the community, also as a “focal point” where the people new to this technology can quickly tune into the pulse of the community. Registering your blog will help build a valuable resource for this growing community.

Regards

egg / June 20th, 2006, 9:33 am / #

Post a comment