On Sat, Sep 10, 2011 at 21:51, Dave Aronson <googlegroups2d...@davearonson.com> wrote:
> Try this: > > u.matches.index { |match| match.user_id == 2 } == nil On rereading this and then seeing the thread on asking questions, methinks I ought to talk a bit about how I came up with this, and how the rest of you can do likewise when stuck in a simliar situation as the OP. As you may recall, he wanted to use find, but the method returned an Array, which doesn't have a find method. I figured, there's *got* to be *some* useful method on Array. There wasn't one that I immediately recalled offhand. So I went to the docs for the Array class, and looked at the methods. Of course there were things like map/collect, that could be used to loop over it and return true if it found a match or false if it hit the end, or select that could be used to construct an array of "hit" elements. But I figured there was probably a cleaner solution, that would still be clear and concise, as a one-liner. I didn't know index would take a block... and stumbling across that was the key. Upshot: if you want to use method X, that works on class A, but you're stuck with class B, take a quick stroll through the methods on B, and see if there's something that can at least be used in a similar way. If not, maybe there's even something better -- you don't have to solve all similar problems the exact same way. -Dave -- Main Web Site: davearonson.com | LOOKING FOR WORK, Programming Blog: codosaur.us | preferably RoR, in NoVa/DC; Excellence Blog: dare2xl.com | see main web site. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-talk+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.