<p>I have a method that can return either a single object or a collection of objects. I want to be able to run object.collect on the result of that method whether or not it is a single object or a collection already. How can i do this?</p>

<pre><code>profiles = ProfileResource.search(params)
output = profiles.collect do | profile |
    profile.to_hash
end
</code></pre>

<p>If profiles is a single object, I get a NoMethodError exception when I try to execute collect on that object.</p>

[{'answer_id': 79416, 'author': 'Matt Haley', 'author_id': 14142, 'author_profile': 'https://Stackoverflow.com/users/14142', 'pm_score': 1, 'selected': False, 'text': '<pre><code>profiles = [ProfileResource.search(params)].flatten\noutput = profiles.collect do |profile|\n    profile.to_hash\nend\n</code></pre>\n'}, {'answer_id': 79427, 'author': 'ctcherry', 'author_id': 10322, 'author_profile': 'https://Stackoverflow.com/users/10322', 'pm_score': 0, 'selected': False, 'text': '<p>In the <code>search</code> method of the <code>ProfileResource</code> class, always return a collection of objects (usually an Array), even if it contains only one object.</p>\n'}, {'answer_id': 79457, 'author': 'Mike Tunnicliffe', 'author_id': 13956, 'author_profile': 'https://Stackoverflow.com/users/13956', 'pm_score': 4, 'selected': True, 'text': '<p>Careful with the flatten approach, if search() returned nested arrays then unexpected behaviour might result.</p>\n\n<pre><code>profiles = ProfileResource.search(params)\nprofiles = [profiles] if !profiles.respond_to?(:collect)\noutput = profiles.collect do |profile|\n    profile.to_hash\nend\n</code></pre>\n'}, {'answer_id': 79502, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': "<p>Here's a one Liner:</p>\n\n<pre><code>[*ProfileResource.search(params)].collect { |profile| profile.to_hash }\n</code></pre>\n\n<p>The trick is the splat (*) that turns both individual elements and enumerables into arguments lists (in this case to the new array operator)</p>\n"}, {'answer_id': 79506, 'author': 'Zakaria', 'author_id': 3370, 'author_profile': 'https://Stackoverflow.com/users/3370', 'pm_score': 0, 'selected': False, 'text': '<p>If the collection is an Array you could use this technique</p>\n\n<pre><code>profiles = [*ProfileResource.search(params)]\noutput = profiles.collect do | profile |\n    profile.to_hash\nend\n</code></pre>\n\n<p>That would guaranteed your profiles is always an array.</p>\n'}, {'answer_id': 79512, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': '<pre><code>profiles = ProfileResource.search(params)\noutput = Array(profiles).collect do |profile|\n    profile.to_hash\nend\n</code></pre>\n'}, {'answer_id': 79581, 'author': 'patrickyoung', 'author_id': 3701, 'author_profile': 'https://Stackoverflow.com/users/3701', 'pm_score': 0, 'selected': False, 'text': '<p>You could first check to see if the object responds to the "collect" method by using "pofiles.respond_to?".  </p>\n\n<p>From <a href="http://www.ruby-doc.org/docs/ProgrammingRuby/" rel="nofollow noreferrer">Programming Ruby</a></p>\n\n<blockquote>\n  <p>obj.respond_to?(\n  aSymbol, includePriv=false ) -> true\n  or false  </p>\n  \n  <p>Returns true if obj responds to the\n  given method. Private methods are\n  included in the search only if the\n  optional second parameter evaluates to\n  true.</p>\n</blockquote>\n'}, {'answer_id': 81655, 'author': 'Farrel', 'author_id': 7889, 'author_profile': 'https://Stackoverflow.com/users/7889', 'pm_score': 0, 'selected': False, 'text': '<p>You can use the Kernel#Array method as well.</p>\n\n<pre><code>profiles = Array(ProfileResource.search(params))\noutput = profiles.collect do | profile |\n    profile.to_hash\nend\n</code></pre>\n'}, {'answer_id': 103471, 'author': 'fatgeekuk', 'author_id': 17518, 'author_profile': 'https://Stackoverflow.com/users/17518', 'pm_score': 0, 'selected': False, 'text': '<p>Another way is to realise that Enumerable requires that you supply an each method.</p>\n\n<p>So. you COULD mix in Enumerable to your class and give it a dummy each that works....</p>\n\n<pre>\nclass YourClass\n  include Enumerable\n\n  ... really important and earth shattering stuff ...\n\n  def each\n    yield(self) if block_given?\n  end\nend\n</pre>\n\n<p>This way, if you get back a single item on its own from the search, the enumerable methods will still work as expected.</p>\n\n<p>This way has the advantage that all the support for it is inside your class, not outside where it has to be duplicated many many times.</p>\n\n<p>Of course, the better way is to change the implementation of search such that it returns an array irrespective of how many items is being returned.</p>\n'}]
['https://Stackoverflow.com/questions/79352', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1486/']