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 6786797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:17:16+00:00 2026-05-26T17:17:16+00:00

I have a bit of an odd use-case for a Ruby Enumerable, it seems.

  • 0

I have a bit of an odd use-case for a Ruby Enumerable, it seems. I am attempting to do something like the following:

result = my_strategies.some_method do |strategy|
    strategy.get_result
end

The method some_method is just a placeholder, but is the basis behind the rest of this question.

The enumerable my_strategies contains an ordered list of strategies for retrieving a value from a remote service; a more preferable strategy is run before a less preferable strategy,

Sometimes the more preferable strategy will fail, in a way that retries alone won’t correct. In that case, the strategy will return nil.

I can see a way of doing this by relying on an each block, thusly:

result = nil

my_strategies.each do |strategy|
    result = strategy.get_result
    if not r.nil?
        break
    end
end

This seems unnecessarily noisy. I’m wondering if there is a method I can substitute some_method for in my first example; something similar to .any?, but returning the value that caused the block to be true, instead of just returning true.

Alternate approaches to what I am trying to do are also welcome.

EDIT: I originally asked this question because I had tried this block of code:

result = my_strategies.find do |strategy|
    strategy.get_result
end

Except that this returned me the strategy that succeeded, instead of the value it returned when it did. I don’t care about which strategy got me the value, I just want to know what the value is.

  • 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-26T17:17:17+00:00Added an answer on May 26, 2026 at 5:17 pm

    Your need is very common but unfortunately there is no such abstraction in the core. However, Facets guys identified this gap a long time ago and implemented Enumerable#find_yield (a.k.a Enumerable#map_detect):

    result = my_strategies.map_detect { |strategy| strategy.get_result }
    

    Or simply: result = my_strategies.map_detect(&:get_result). Ruby 2.0 implements lazy enumerables (for early versions use enumerable-lazy) so now we can write:

    result = my_strategies.lazy.map(&:get_result).reject(&:nil?).first
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bit of html like so: <a href=#somthing id=a1><img src=something /></a> <a
This is a bit of an odd situation. Here's what's happening. So, we have
This is an odd problem, so I have to provide a bit of background.
I have a bit of an odd problem, and I'm struggling to track down
Okay, this problem is a bit odd. I have an application built in VS2010
G'day guys, I have a bit of an odd request for a dynamic form.
i have bit of code that causes an underflow: var t1, t2, delta: DWORD:
I have this bit of javascript written with jQuery 1.2.5. It's contained inside the
I have a bit of code that basically reads an XML document using the
I have a bit of code that passes around a ton of objects and

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.