Upload
gteodorescu
View
217
Download
0
Embed Size (px)
Citation preview
8/12/2019 7 Daily Use Cases of Ruby Array
1/4
7 daily use cases of Ruby Array
7 Replies
Ruby and Ruby on Rails come with great reference documentation (Ruby doc,Rails doc, oralternatives such as APIdock, or the recent omnirefcovering 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 = [ '[email protected]', '[email protected]']existing_emails = [ '[email protected]', '[email protected]', '[email protected]']
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 dulicating entries?
ou want to get a uni+ue list of twitter accounts id followed by two persons
folloeds# = [ #, %, )]
http://blog.8thcolor.com/2014/02/7-daily-use-cases-of-ruby-array/#commentshttp://ruby-doc.org/http://api.rubyonrails.org/http://api.rubyonrails.org/http://apidock.com/http://www.omniref.com/http://www.ruby-doc.org/core-2.1.0/Array.html#method-i-2Dhttp://www.ruby-doc.org/core-2.1.0/Array.html#method-i-26http://blog.8thcolor.com/2014/02/7-daily-use-cases-of-ruby-array/#commentshttp://ruby-doc.org/http://api.rubyonrails.org/http://apidock.com/http://www.omniref.com/http://www.ruby-doc.org/core-2.1.0/Array.html#method-i-2Dhttp://www.ruby-doc.org/core-2.1.0/Array.html#method-i-268/12/2019 7 Daily Use Cases of Ruby Array
2/4
folloeds% = [ %, *, +]
all_folloeds = folloeds# folloeds%
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="0elgium1,
name="6iet, location="0elgium1, name="7ran8ois, location="7rance1, name="2ohn, location="3nited 4tates of 5merica1 ]
Reference /numerable0sort1by
". How to kee ob#ects in an Array that are uni$ue with
resect 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
2
http://www.ruby-doc.org/core-2.1.0/Array.html#method-i-7Chttp://www.ruby-doc.org/core-2.1.0/Enumerable.html#method-i-sort_byhttp://www.ruby-doc.org/core-2.1.0/Array.html#method-i-7Chttp://www.ruby-doc.org/core-2.1.0/Enumerable.html#method-i-sort_by8/12/2019 7 Daily Use Cases of Ruby Array
3/4
products = [6roduct.ne(#, #,6roduct.ne(%, %,6roduct.ne(), ),6roduct.ne(*, #,
6roduct.ne(+, ),6roduct.ne(9, +,]
products = products.uni: &category_id
IR! %utput
=" [ ;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
defmethod; K
[Lproducts]
6
http://www.ruby-doc.org/core-2.1.0/Array.html#method-i-uniqhttp://ruby-doc.org/core-2.1.0/Enumerable.html#method-i-grephttp://www.ruby-doc.org/core-2.1.0/Array.html#method-i-uniqhttp://ruby-doc.org/core-2.1.0/Enumerable.html#method-i-grep8/12/2019 7 Daily Use Cases of Ruby Array
4/4
end
ou"ll find more e$planations in the !ohidar !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
:
http://batsov.com/articles/2013/06/28/the-elements-of-style-in-ruby-number-3-make-sure-something-is-an-array/http://www.ruby-doc.org/core-2.1.0/Kernel.html#method-i-Arrayhttp://www.ruby-doc.org/core-2.1.0/doc/syntax/calling_methods_rdoc.html#label-Array+to+Arguments+Conversionhttp://www.ruby-doc.org/core-2.1.0/doc/syntax/calling_methods_rdoc.html#label-Array+to+Arguments+Conversionhttp://www.ruby-doc.org/core-2.1.0/doc/syntax/calling_methods_rdoc.html#label-Array+to+Arguments+Conversionhttps://github.com/bbatsov/ruby-style-guidehttp://batsov.com/articles/2013/06/28/the-elements-of-style-in-ruby-number-3-make-sure-something-is-an-array/http://www.ruby-doc.org/core-2.1.0/Kernel.html#method-i-Arrayhttp://www.ruby-doc.org/core-2.1.0/doc/syntax/calling_methods_rdoc.html#label-Array+to+Arguments+Conversionhttps://github.com/bbatsov/ruby-style-guide