Created
July 2, 2015 00:04
-
-
Save willbuilds/8397adf6587bce4723cc to your computer and use it in GitHub Desktop.
Pattern for handling array vs single
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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