DEV Community

Discussion on: GitHub auto README with ruby, github-actions and dev.to API

Collapse
 
pashagray profile image
Pavel Tkachenko

The easiest way is to call sample(n) method on your posts where n is number of random posts you want to pick.

posts = JSON.parse(response.body).sample(5).map do |article|
Enter fullscreen mode Exit fullscreen mode

It works with any array.

[1, 2, 3, 4, 5].sample(3) #=> 3, 1, 5
[1, 2, 3, 4, 5].sample(3) #=> 5, 4, 1
[1, 2, 3, 4, 5].sample(3) #=> 2, 4, 3
Enter fullscreen mode Exit fullscreen mode
Collapse
 
omarkhatib profile image
Omar

I will take it in consideration if I need to implement it , I edit it to get last post only with picture. Thanks for the script <3