0

I have a method that is basically a loop and it calls itself at the end each time. What is the best way for the method to not call itself when the date reaches a certain point? Each iteration through adds 1 day and basically processes stats for that day. It looks like the below:

def loop(start_day)
  date = start_day

  #do a bunch of stuff

  date = date +1.day

  if date > Time.now
    puts "loop should be over"
  end

  loop(date)
end
4
  • Correct me if I'm wrong but won't using recursion keep compounding the memory usage depending on the variables you are setting in #do a bunch of stuff whereas an until loop will clear out the variables with each loop? Commented Apr 22, 2013 at 16:21
  • @CharlesCaldwell It goes without saying (at least, I thought...) that this would be a silly thing to do in real life. I assumed this was just an exercise. Commented Apr 22, 2013 at 16:45
  • 1
    Note that you do not need recursion in this scenario. You could create a range for the dates and then just iterate through each date of the range. Commented Apr 22, 2013 at 16:51
  • creating a range is more effective? Commented Apr 22, 2013 at 20:39

4 Answers 4

3

Each iteration through adds 1 day

That's not true for the code you've posted. In your code you add 1 day to the start date once and then you keep processing the same date over and over again because you recurse on the old date (start_date), not the incremented date (date).

What is the best way for the method to not call itself when the date reaches a certain point?

Just put the recursive call inside an if or, in this case, inside of the else of the if that you already have.

Sign up to request clarification or add additional context in comments.

Comments

3

Since you set date to start_date immediately, it seems there's no point in having both. Here's the more canonical form of doing recursion:

def loop(date)
  return if date > Time.now
  #do a bunch of stuff
  loop(date + 1.day)
end

Update: If it's not obvious to you that recursion isn't necessary here, in real life, it would make more sense to do something like this:

def process_from(date)
  while date <= Time.now
    # Process date
    date += 1.day
  end
end

Comments

1

What about this?

def loop(start_day)
  return "loop should be over" if start_day >= Time.now
  #...
  loop(modified_date)
end

or...

def loop(start_day)
  date = start_day.dup
  time = Time.now
  date += 1.day while date <= time
  'loop should be over'
end

It seems like you want to iterate over all days from starting date to today. Then maybe this is even more simple:

def map_date(date)
  (date.to_date..Date.today).map do |d|
    d.strftime("Today is %A")
  end
end

Comments

0

You must have base case (stop case) for recursive function;

example:

def loop(date)
    if date == certain_point
      return [something_for_stop]
    end

    loop(date - 1)
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.