Skip to content

Instantly share code, notes, and snippets.

@willbuilds
Created July 2, 2015 00:04
Show Gist options
  • Save willbuilds/8397adf6587bce4723cc to your computer and use it in GitHub Desktop.
Save willbuilds/8397adf6587bce4723cc to your computer and use it in GitHub Desktop.
Pattern for handling array vs single
# value arg could be Array of objects, or single object
def print_this(value)
print = ''
(value.is_a?(Array) ? value : [value]).each do |v|
print += v
end
puts print
end
# eg
print_this('hello') -> 'hello'
print_this(['hello ', 'how ', 'are ', 'you']) -> 'hello how are you'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment