URL resolver for Users, Lessons, and Series My Resolver.resolve class returns a hash of objects and I would like to return a specific object if it exists. Is there a more concise way to write this?

def resolve_url(url)
    # Resolver library now returns a hash of objects
    # Using this helper method to preserve the old functionality of returning a single object
    # Order of old return types is Lesson -&gt; Series -&gt; User
    objects = Resolver.resolve(url, :only =&gt; [User, Lesson, Series])
    if objects[Lesson]
      objects[Lesson]
    elsif objects[Series]
      objects[Series]
    elsif objects[User]
      objects[User]
    else
      nil
    end
  end
end
I might as well post this then:

def resolve_url(url)
    # Resolver library now returns a hash of objects
    # Using this helper method to preserve the old functionality of returning a single object
    # Order of old return types is Lesson -&gt; Series -&gt; User
    only = [Lesson, Series, User]
    objects = Resolver.resolve(url, :only =&gt; only)
    objects.values_at(*only).compact[0]
  end
end


So first get only the necessary fields (reusing the list from the resolve call), remove nils with compact and finally return the first value; if it's empty you still get nil back.

I've also removed my comment since you've updated your post.  FWIW you could also use something like objects[Lesson] or objects[Series] or Objects[User] instead.