Oct 03, 2008 @ 05:39 pm

Pretty easy stuff today, but this is not something you may need to do often so maybe you don’t know how to do it.

Say I have these 2 arrays :

  1. colors1 = [:blue, :red, :green, :orange, :purple]
  2. colors2 = [:yellow, :cyan, :green, :blue, :purple]

Now what if I want a new array that contains only the elements present in both arrays?

  1. beautiful_colors = colors1 & colors2
  2. #beautiful_colors contains [:blue, :green, :purple]

Have a nice weekend!

Bookmark this post : These icons link to social bookmarking sites where readers can share and discover new web pages.
  • DZone
  • Reddit
  • del.icio.us
  • Digg
  • Furl
  • Technorati
  • StumbleUpon
Rate this post :
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 4.67 out of 5)
Loading ... Loading ...
Posted under : short & sweet
No Comments


Leave a comment
Name (required)
Email (will not be publish) (required)
Website