How to leave this DRY code block in Ruby

Asked

Viewed 70 times

1

I got this code pad right here:

if contract.amendments.empty?
  if Date.today - contract.due_date >= 30 || Date.today + contract.due_date <= 30
    @start_date = contract.due_date + 1
    @end_date = contract.due_date + 365
  elsif Date.today - contract.due_date < 30
    @start_date = Date.today
    @end_date = contract.end_date
  end
else
  last_amendment = contract.amendments.last
  if Date.today - last_amendment.due_date >= 30 || Date.today + last_amendment.due_date <= 30
    @start_date = last_amendment.end_date + 1
    @end_date = last_amendment.end_date + 365
  elsif Date.today - last_amendment.end_date < 30
    @start_date = Date.today
    @end_date = last_amendment.end_date
  end
end

And I would like to let you DRY, but I’m not getting it. I’m a beginner in Ruby

  • I see you never voted for anything. Did you know you can vote for all the posts on the site? Vote one whatever you think is useful for you and others. See more on the [tour].

1 answer

1


The two codes are not exactly equivalent, the condition of the elsif is very different, if they were the same (could be an error in the presented code) would be like this:

if contract.amendments.empty?
  due_date = contract.due_date
  end_date = contract.end_date
else
  due_date = contract.amendments.last.due_date
  end_date = contract.amendments.last.end_date
end
if Date.today - due_date >= 30 || Date.today + due_date <= 30
  @start_date = due_date + 1
  @end_date = end_date + 365
elsif Date.today - end_date < 30
  @start_date = Date.today
  @end_date = end_date
end

If it wasn’t a mistake, we can still fix it:

if contract.amendments.empty?
  due_date = contract.due_date
  end_date = contract.end_date
  condition = Date.today - contract.due_date < 30
else
  due_date = contract.amendments.last.due_date
  end_date = contract.amendments.last.end_date
  condition = Date.today - last_amendment.end_date < 30
end
if Date.today - due_date >= 30 || Date.today + due_date <= 30
  @start_date = end_date + 1
  @end_date = end_date + 365
elsif condition
  @start_date = Date.today
  @end_date = end_date
end

I put in the Github for future reference.

  • I ended up missing yes. in the last_amendment part, it would be the end_date method, not due_date

  • In this case, when I executed the code, I gave an error: 'Expected Numeric'. Even after I converted the end_date variable to the date type.

  • Give more details of the error, only with this I can not see what may be wrong. In theory should not need to convert anything. Unless you have some quirk in Ruby that I don’t know or you’ve slid in the change of some chunk, I don’t see why it could be wrong.

  • if Contract.amendments.Empty? end_date = Date.new(Contract.due_date.year, Contract.due_date.Month, Contract.due_date.day) condition = Date.Today - Contract.due_date < 30 Else end_date = Date.new(last_amendment.end_date.year, last_amendment.end_date.month, last_amendment.end_date.day)&#xA; condition = Date.today - last_amendment.end_date < 30&#xA; end &#xA;&#xA; if Date.today - end_date >= 30 || Date.today + end_date <= 30

  • In the last if, gave the error

  • Unfortunately, just by looking at the code like this, I can’t tell you why it’s wrong. I saw that the code is not exactly the same as the original, so some error may have been introduced there.

  • It is that at the time of subtraction, he says he expects a numerical value. But in my view, he is right :(

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.