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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:36:11+00:00 2026-05-16T15:36:11+00:00

I currently have the following code: events.detect do |event| #detect does the block until

  • 0

I currently have the following code:

events.detect do |event|

#detect does the block until the statement goes false

  self.event_status(event) == "no status"

end

What this does is output the instance of event (where events is a string of different Models that all collectively call Events) when the event_status method outputs a “no status”.

I would like the output to also include the value for delay where:

delay = delay + contact.event_delay(event)

event_delay method hasn’t been written, but it would be similar (maybe redundant but I’ll deal with that later) to event_status in looking at the delay between when an event was done and when it was supposed to be done.

Here is how event_status looks currently for reference:

  def event_status target
  # check Ticket #78 for source

    target_class= target.class.name
    target_id   = target_class.foreign_key.to_sym

    assoc_name  = "contact_#{target_class.tableize}"

    r = send(assoc_name).send("find_by_#{target_id}", target.id) 
    return "no status" unless r
    "sent (#{r.date_sent.to_s(:long)})" 
  end

My concept of output should be [event,delay] so that, for example, I can access it as Array[:event] or Array[:delay] to get at the value.

****I was thinking maybe I should use yield on a method, but haven’t quite put the pieces together (should the block passed to the method be the delay =+ for example, I think it is).**

I am not wed to the .detect method, it’s what I started with and it appears to work, but it isn’t allowing me to run the tally alongside it.

  • 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-16T15:36:12+00:00Added an answer on May 16, 2026 at 3:36 pm

    It’s not entirely clear what you’re asking for, but it sounds like you’re trying to add up a delay until you reach a certain condition, and return the record that triggered the condition at the same time.

    You might approach that using Enumerable#detect like you have, but by keeping a tally on the side:

    def next_event_info
      next_event = nil
      delay = 0
    
      events.detect do |event|
        case (self.event_status(event))
        when "no status"
          true
        else
          delay += contact.event_delay(event)
          false
        end
      end
    
      [ next_event, delay ]
    end
    

    Update for if you want to add up all delays for all events, but also find the first event with the status of "no status":

    def next_event_info
      next_event = nil
      delay = 0.0
    
      events.each do |event|
        case (self.event_status(event))
        when "no status"
          # Only assign to next_event if it has not been previously
          # assigned in this method call.
          next_event ||= event
        end
    
        # Tally up the delays for all events, converting to floating
        # point to ensure they're not native DB number types.
        delay += contact.event_delay(event).to_f
      end
    
      {
        :event => next_event,
        :delay => delay
      }
    end
    

    This will give you a Hash in return that you can interrogate as info[:event] or info[:delay]. Keep in mind to not abuse this method, for example:

    # Each of these makes a method call, which is somewhat expensive
    next_event = next_event_info[:event]
    delay_to_event = next_event_info[:delay]
    

    This will make two calls to this method, both of which will iterate over all the records and do the calculations. If you need to use it this way, you might as well make a special purpose function for each operation, or cache the result in a variable and use that:

    # Make the method call once, save the results
    event_info = next_event_info
    
    # Use these results as required
    next_event = event_info[:event]
    delay_to_event = event_info[:delay]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 509k
  • Answers 509k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer yes: The purpose of Weight is to ensure searching does… May 16, 2026 at 4:36 pm
  • Editorial Team
    Editorial Team added an answer // Here... fstat(fd, &fs); // And here... n=read(fd, buf, 10);… May 16, 2026 at 4:36 pm
  • Editorial Team
    Editorial Team added an answer Write # encoding: utf-8 on top of that file. That… May 16, 2026 at 4:36 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

currently I have following code: home.php <form name='myformname' id='myformid'> <input type='text' name='mytext1' value='abc'> <input
I have the following code: Private Sub txtFileFromLocation_TextChanged(ByVal sender As System.Object, ByVal e As
I have the following ASPX code: <asp:ScriptManager ID=ScriptManager1 runat=server /> <asp:UpdatePanel runat=server ID=UpdatePanel UpdateMode=Conditional>
I'd like to have access to the bytecode that is currently running or about
we're currently developing an application that makes extensive use of popup windows(*) and have
I am trying to implement the following functionality: Flex data grid has 1 default
I am currently developing Unit Tests for a Javascript method that detects the readiness
I have a UIView that contains a number of CALayer subclasses. I am using
I am making a fighting game in Flash and while I have everything running,
I am relatively new to programming, as you will soon see... I have 2

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.