Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



I 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.


Most often I do

  var_that_might_be_nil.presence || default
reads better imo


"presence" isn't Ruby, but Rails/ActiveSupport and your example acts very differently in the case of empty strings, empty arrays, hashes etc..


yes, and?


So it's often a) not available, b) semantically not an alternative.

In particular it very specifically does not return the default only when the object is nil, and I have plenty of code where people thinking they're the same would cause serious breakage.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: