News for hackers. Stuff that matters.
Re:Ruby 2.6 by abecedarius on Monday December 24, 2018 @01:13PM (#18753370)
Can you recommend a post/article explaining some of what makes Ruby's collections especially pleasant? Studying Ruby's stdlib design has been on my 'someday' list, but there's so much else there...
Re:Ruby 2.6 by ajmurmann on Monday December 24, 2018 @03:44PM (#18754176)
It's mostly how complete the API is and how consistent. For example: * Hash#merge takes a block for conflict resolution. I use this all the time for aggregating counts. * #sample * #unique * Everything has a change in place option by appending "!"
Re:Ruby 2.6 by mmahemoff on Monday December 24, 2018 @04:01PM (#18754250)
Also, chaining is extremely concise. e.g. "products.map(&:cost).sort.last"
Re:Ruby 2.6 by klohto on Tuesday December 25, 2018 @06:12AM (#18756982)
I couldn't resist...

Your example could be improved by using "products.sort_by(&:cost).last"

I try to not use map because often there are methods that already implement filtering. Gotta love Ruby readability.

Re:Ruby 2.6 by viraptor on Tuesday December 25, 2018 @08:05AM (#18757251)
I think you need `products.sort_by(&:cost).last.cost` for the same result.
Re:Ruby 2.6 by klohto on Tuesday December 25, 2018 @10:20AM (#18757700)
You’re correct!
Re:Ruby 2.6 by zem on Monday December 24, 2018 @03:49PM (#18754202)
one beautiful design decision ruby made really on was that every method would have the option to take a block of code when it was invoked. the code block gets passed to the method as a closure. this makes writing collection apis very pleasant, since so many of them involve passing in a single closure to operate on each element.