4 ms·
I code ruby (not exclusively) for like 20 years now, that must be the reason I dont find this odd at all. Which example from the list is weird? I would not wr
by entropie 3y ago
I code ruby (not exclusively) for like 20 years now, that must be the reason I dont find this odd at all.
Which example from the list is weird?
I would not write
var_that_might_be_nil.to_h
I find this more understandable
var_that_might_be_nil || {}
But I find both acceptable and fine.
- sethrin 3y agoMost often I do var_that_might_be_nil.presence || default reads better imo
- vidarh 3y ago"presence" isn't Ruby, but Rails/ActiveSupport and your example acts very differently in the case of empty strings, empty arrays, hashes etc..
- vidarh 3y agoI agree if it is known that the value can only be Hash or nil. But to_h often makes sense when you need a Hash, and there are other possible values too.