Sie sind auf Seite 1von 4

7 daily use cases of Ruby Array

7 Replies 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) !ut 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 #he documentation e$plains what each piece can do, not how you can use it (and clearly not which you should use for a given use case) %f course, that"s the same for tutorial and books &opefully, there is 'tack%verflow !elow, 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?


(ou want to check that all imported emails already e$ist 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!

IR! %utput)
already imported =" nil

Reference) Array *ifference

2. How to find whats elements are common to two Arrays?


(ou want to find the common tags of two blog posts
tags_post# = [ 'ru$y', 'rails', 'test' ] tags_post% = [ 'test', 'rspec' ] common_tags = tags_post# & tags_post%

IR! %utput)
=" ['test']

Reference) 'et Intersection

3. How to merge two Arrays without du licating entries?


(ou want to get a uni+ue list of twitter accounts id followed by two persons
follo(eds# = [ #, %, ) ]

follo(eds% = [ %, *, + ] all_follo(eds = follo(eds# , follo(eds%

IR! %utput)
=" [#, %, ), *, +]

Reference) 'et -nion

!. How to sort an Array of Hashes?


(ou retrieve data from a third party API that returns an Array of &ashes as following)
data = [ name. '/hristophe', location. '0elgium' 1, name. '2ohn', location. '3nited 4tates of 5merica' 1, name. '6iet', location. '0elgium' 1, name. '7ran8ois', location. '7rance' 1 ]

.hen displaying those data, you want to sort them by location (ou can do it with)
data.sort_$y - ,hsh, hsh[.location] 1

IR! %utput)
=" [ -.name="'/hristophe', .location="'0elgium'1, -.name="'6iet', .location="'0elgium'1, -.name="'7ran8ois', .location="'7rance'1, -.name="'2ohn', .location="'3nited 4tates of 5merica'1

Reference) /numerable0sort1by

". How to kee ob#ects in an Array that are uni$ue with res ect to one attribute?
In an online shop, on the home page, you need to show one product per category
6roduct = 4truct.ne((.id, .category_id

products = [ 6roduct.ne((#, 6roduct.ne((%, 6roduct.ne((), 6roduct.ne((*, 6roduct.ne((+, 6roduct.ne((9, ]

# % ) # ) +

, , , , , ,

products = products.uni: &.category_id

IR! %utput)
=" [ ;<struct ;<struct ;<struct ;<struct ] 6roduct 6roduct 6roduct 6roduct id=#, id=%, id=), id=9, category_id=#", category_id=%", category_id=)", category_id=+"

Reference) Array0uni+

%. How to filter an Array with a &tring?


(ou"ve retrieved a list of books and you want to filter them with a 'tring)
$oo=s = [ '>he ?u$y 6rogramming @anguage', '6rogramming ?u$y #.A & %.B. >he 6ragmatic 6rogrammersC' Duide (>he 7acets of ?u$y ', '6ractical E$ject-Eriented Fesign in ?u$y. 5n 5gile 6rimer', 'Glo:uent ?u$y', '?u$y on ?ails >utorial. @earn He$ FeIelopment (ith ?ails' ] $oo=s = $oo=s.grep(J[?r]ailsJ

IR! %utput)
=" ['?u$y on ?ails >utorial. @earn He$ FeIelopment (ith ?ails']

Reference) /numerable0grep

7. How to always get an Array?


In a method you work with products and you eventually return one or several !ut when you get only one, it"s not an Array (ou can deal very smoothly with both cases with Array() or 345)
def method ; K [Lproducts]

end

(ou"ll find more e$planations in the !o7hidar !atsov"s post Reference) 8ernel0Array, splat operator, Ruby 'tyle guide

'hats 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 .hat"s yours9 .hat are your typical daily use cases of Array and beautiful Ruby to resolve them9

Das könnte Ihnen auch gefallen