Crystal: Add `#each` and `#map` to Object and Nil

Created on 16 Nov 2020  路  3Comments  路  Source: crystal-lang/crystal

I was going over some old episodes from Ruby Tapas and really like this idea from the Safe Navigation series.

https://www.rubytapas.com/2015/11/23/episode-361-safe-navigation-part-1-reliable-associations/
https://www.rubytapas.com/2015/11/26/episode-362-safe-navigation-part-2-try-try-again/
https://www.rubytapas.com/2015/11/30/episode-363-safe-navigation-part-3-everything-is-optional/
https://www.rubytapas.com/2015/12/03/episode-364-safe-navigation-part-3-refinements/
https://www.rubytapas.com/2015/12/07/episode-365-safe-navigation-part-5-adapter/

This addresses a few problems but I like how you can safely navigate through objects and arrays.

This involved adding #each and #map to Object and Nil. I find the solution to be a little off putting but maybe another name to this solution would be good. Something like #in and #through for #each and #map respectively.

Most helpful comment

Yes, we already have this. #try. arr.try &.map { |v| v + 1}.

All 3 comments

Wouldn't this just hide the problem that you're trying to iterate nil instead of forcing you to actually handle it?

I mean this could be a pattern for handling/dealing with nil but I can see your point. There are other was to handle nil as well.

Yes, we already have this. #try. arr.try &.map { |v| v + 1}.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lbguilherme picture lbguilherme  路  3Comments

Papierkorb picture Papierkorb  路  3Comments

ArthurZ picture ArthurZ  路  3Comments

cjgajard picture cjgajard  路  3Comments

relonger picture relonger  路  3Comments