Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3936046
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:54:00+00:00 2026-05-19T23:54:00+00:00

I have a collection of objects. There are 3 properties in each object ‘id’,

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:54:00+00:00Added an answer on May 19, 2026 at 11:54 pm

    Hmm, this doesn’t quite work if no element is_primary…I’m still thinking…

    c.detect(&:is_primary).name
    

    Ok, how about:

    ((a = c.detect(&:is_primary)) && a.name).to_s
    

    As it happens, it is OK in an erb template for the <%= expression to return nil, that just results in an empty string, so for that case you can use:

    (a = c.detect(&:is_primary)) && a.name
    

    Update: Responding to the first comment, I do have a test case that I didn’t post…

    class A; attr_accessor :is_primary, :name, :id; end
    t = [A.new, A.new, A.new, (a = A.new; a.name = 'xyz'; a.is_primary = true; a)]
    
    puts (a = t.detect(&:is_primary)) && a.name
    
    puts ((a = [].detect(&:is_primary)) && a.name).to_s
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Collection objects, I want to associate each object of these two
I have a collection of objects and inside of each object I have a
I have a collection of objects, each of which holds a set of name-value
I have a collection of permissions. Each permission would have three properties: Id, Name,
I have a collection of MDI forms, where there are different objects (Form1,Form2 etc).
I have a collection of objects which describe an image-name, its size and it's
So I have a collection of objects. The exact type isn't important. From it
I have a large collection of custom objects that I have retrieved from a
In my application, I have three collection objects which store data. The data which
I have a Queue that contains a collection of objects, one of these objects

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.