Ruby and Ruby on Rails come with great reference documentation (Ruby doc, Rails doc, or alternatives such as APIdock, or the recent omniref covering also all gems). But when you need to find how to check if one array has all elements of another, you won't find the answer in the doc because it's not use case oriented. The documentation explains what each piece can do, not how you can use it (and clearly not which you should use for a given use case). Of course, that's the same for tutorial and books. Hopefully, there is StackOverflow. Below, I share 7 common use cases of Array I met very often and should be useful to you.

1. How to check if one Array has all elements of another?

You want to check that all imported emails already exist in a contact list.

imported_emails = [ 'john@doe.com', 'janet@doe.com' ]
existing_emails = [ 'john@doe.com', 'janet@doe.com' , 'fred@mercury.com' ]

puts 'already imported' if (imported_emails - existing_emails).empty?

IRB Output:

already imported
=> nil

Reference: Array Difference

2. How to find what's elements are common to two Arrays?

You want to find the common tags of two blog posts.

tags_post1 = [ 'ruby', 'rails', 'test' ]
tags_post2 = [ 'test', 'rspec' ]

common_tags = tags_post1 & tags_post2

IRB Output:

 => ["test"]

Reference: Set Intersection

3. How to merge two Arrays without duplicating entries?

You want to get a unique list of twitter accounts id followed by two persons.

followeds1 = [ 1, 2, 3 ]
followeds2 = [ 2, 4, 5 ]

all_followeds =  followeds1 | followeds2

IRB Output:

 => [1, 2, 3, 4, 5]

Reference: Set Union

4. How to sort an Array of Hashes?

You retrieve data from a third party API that returns an Array of Hashes as following:

data = [
 {
    name: 'Christophe',
    location: 'Belgium'
 },
 {
    name: 'John',
    location: 'United States of America'
 },
 {
    name: 'Piet',
    location: 'Belgium'
 },
 {
    name: 'François',
    location: 'France'
 }
]

When displaying those data, you want to sort them by location. You can do it with:

data.sort_by { |hsh| hsh[:location] }

IRB Output:

 => [
     {:name=>"Christophe", :location=>"Belgium"},
     {:name=>"Piet", :location=>"Belgium"},
     {:name=>"François", :location=>"France"},
     {:name=>"John", :location=>"United States of America"}
    ]

Reference: Enumerable#sort_by

5. How to keep objects in an Array that are unique with respect to one attribute?

In an online shop, on the home page, you need to show one product per category.

Product = Struct.new(:id, :category_id)

products = [
 Product.new(1, 1),
 Product.new(2, 2),
 Product.new(3, 3),
 Product.new(4, 1),
 Product.new(5, 3),
 Product.new(6, 5),
]

products = products.uniq &:category_id

IRB Output:

 => [
     #<struct Product id=1, category_id=1>,
     #<struct Product id=2, category_id=2>,
     #<struct Product id=3, category_id=3>,
     #<struct Product id=6, category_id=5>
    ]

Reference: Array#uniq

6. How to filter an Array with a String?

You've retrieved a list of books and you want to filter them with a String:

books = [
 'The Ruby Programming Language',
 'Programming Ruby 1.9 & 2.0: The Pragmatic Programmers
#039; Guide (The Facets of Ruby)',
 'Practical Object-Oriented Design in Ruby: An Agile Primer',
 'Eloquent Ruby',
 'Ruby on Rails Tutorial: Learn Web Development with Rails'
]

books = books.grep(/[Rr]ails/)

IRB Output:

 => ["Ruby on Rails Tutorial: Learn Web Development with Rails"]

Reference: Enumerable#grep

7. How to always get an Array?

In a method you work with products and you eventually return one or several. But when you get only one, it's not an Array. You can deal very smoothly with both cases with Array() or [*]:

def method
 # …

 [*products]
end

You'll find more explanations in the Bozhidar Batsov's post.

Reference: Kernel#Array, splat operator, Ruby Style guide

What's your daily use cases of Array?

Ruby is full of treasures. It takes some times to know them and find how you can use them. What's yours? What are your typical daily use cases of Array and beautiful Ruby to resolve them?

Update: Cesar did the same exercise in Javascript. Look at his Gist, very interesting.