Ruby: Use `Array(thing)` instead of `thing || []`

Last updated: 22 days ago

Published: almost 2 years ago

raw source | baked source

I learned something recently. Our code used to be peppered with this:

1result = something.else || []

Or variations on the theme:

1a_string = (something.else || []).join(',')

Turns out you can just

1result = Array(something.else)
2a_string = Array(something.else).join(',')

Minor convenience, but lovely nonetheless.


Comments (0)
Add a comment