Beruflich Dokumente
Kultur Dokumente
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
IR! %utput)
already imported =" nil
IR! %utput)
=" ['test']
IR! %utput)
=" [#, %, ), *, +]
.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
# % ) # ) +
, , , , , ,
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+
IR! %utput)
=" ['?u$y on ?ails >utorial. @earn He$ FeIelopment (ith ?ails']
Reference) /numerable0grep
end
(ou"ll find more e$planations in the !o7hidar !atsov"s post Reference) 8ernel0Array, splat operator, Ruby 'tyle guide