I have a collection of objects. There are 3 properties in each object
‘id’, ‘name’, ‘is_primary’
The collection of objects will usually have anywhere from 1 to 5 objects.
What I want to do is check the collection to see if is_primary is true. If so output the name, or at least return it.
I want to do this in 1 line of code if possible. I am trying to slim up this one line for erb output in rails. Later in the page i’ll output them all. I thought I had it, but if I return nil it adds extra space which shifts all the html oddly.
Thanks.
Hmm, this doesn’t quite work if no element
is_primary…I’m still thinking…Ok, how about:
As it happens, it is OK in an erb template for the
<%=expression to returnnil, that just results in an empty string, so for that case you can use:Update: Responding to the first comment, I do have a test case that I didn’t post…