Reid Tattersall
Reid Tattersall

Reputation: 13

How to find the difference between two arrays after calling a method

I'm trying to subtract the earliest date from two arrays:

def days_pending
  start = [object.created_at]
  finish = [Time.now.strftime('%Y-%m-%d')]
  if object.app_sign_date
    start.push(object.app_sign_date)
  end
  if object.submitted_date
    start.push(object.submitted_date)
  end
  if object.inforce_date
    finish.push(object.inforce_date)
  end
  if object.closed_date
    finish.push(object.closed_date)
  end
  finish.min - start.min

I have no problem calling min on the arrays, but have a problem calling the min method and then subtracting. I get NoMethodError: undefined method-' for "2015-01-01":String`.

Upvotes: 0

Views: 114

Answers (3)

seph
seph

Reputation: 6076

If you need the output to be in the form "2015-01-01" you could use parse:

Time.parse(finish.min) - start.min

Upvotes: 0

xuanduc987
xuanduc987

Reputation: 1077

After calling Time.now.strftime('%Y-%m-%d'), what you got is a string. Change to Time.now should work.

Upvotes: 0

Naren Sisodiya
Naren Sisodiya

Reputation: 7288

the array finish has first element as string not the date. you need to add appropriate object instead ie. date or datetime

finish = [Time.now] 

or

finish = [DateTime.now]

Upvotes: 2

Related Questions