Conditionally wrap a variable in an array
These are ugly, but idomatic in many languages:
params = (params.is_a?(Array) ? params : [params])
or
params = [params] unless params.is_a?(Array)
Instead, Ruby provides Array()
, which turns nil
into []
, leaves arrays untouched, and wraps everything else in an array:
# Make sure params is an array
params = Array(params)
Written by meagar
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#