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

  • SEARCH
  • Home
  • 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 7748927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:54:39+00:00 2026-06-01T10:54:39+00:00

I need the following logic. If array contains value , return it else return

  • 0

I need the following logic. If array contains value, return it else return some default value. So far, I’ve found this can be achieved by using delete:

array.delete(value) || default_value

But, obviously, it modifies the array. Is there a similar function like fetch with default which takes an element instead of an index?

PS. I know this can be rewritten as array.include?(value) ? value : default_value, the question is just for the sake of cleaner code.

  • 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-06-01T10:54:41+00:00Added an answer on June 1, 2026 at 10:54 am

    Update: There’s one way I can think of, but that’s harder to read than the ternary:

     (array & [value]).first || default_value
    

    You could monkey-patch that into Array if you are so inclined:

    class Array
      def fetch_value(value, default)
        (self & [value]).first || default
      end
    end
    
    a = *1..3
    a.fetch_value(4, "foo")
    #=> "foo"
    

    Old answer, before I realized you want to use the value for the lookup, not the index:

    Array#fetch takes an optional second argument:

    array.fetch(index, default)
    

    Here’s an example:

    a = *1..3
    a.fetch(4, "foo")
    #=> "foo"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

can someone help with this? I need the following function to do this... $x
I need syntax help with the following code logic: I have a code block
I need the java code snippet for the below logic: Following is a string
I want to implement following logic in my SQL query: If some date is
I have the following url /dir1/dir2/page.php I need to generate a an array of
I create the following array using searchlogic named_scopes: todos = Todo.asset_is(Email).asset_id_is(self.id) For each value
In the following table, I need to find out the login date and how
I need the following authentication script finished. I am weak at php/pdo so I
For a part of a program i need the following 2 methods. The first
Colleagues, Using JPA I need resolve following issue: at database level exits 3 entities

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.