Last active
December 19, 2015 18:58
-
-
Save bkeepers/6002211 to your computer and use it in GitHub Desktop.
I'm looking for a better pattern for defining a method that takes a single object or an array of objects as an argument, does something with them, and then returns either a single object or an Array depending on what was passed to it.
This file contains 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
def dress(dog_or_dogs) | |
dressed_dogs = Array(dog_or_dogs).map {|dog| DogSweater.new(dog) } | |
dog_or_dogs.respond_to?(:each) ? dressed_dogs : dressed_dogs.first | |
end | |
one_dog = dress(Dog.new) | |
all_my_dogs = dress([Dog.new, Dog.new, Dog.new]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@rjackson why call dress from within dress_all rather than dress_one. Calling dress one seems simpler to me