How to make a method return true or false?

My objective is to count the number of conditions that are true. I created an instance variable @cta_count to hold the count.
I would like to make signing_ctas? true if @cta_count > 0. This is not working. Why? I thought the last if statement would make the signing_ctas true or false.

def signing_ctas?
@cta_count = 0

if !(@order.already_a_club_member
  @cta_count+=1
end

if @order.copayed? && @order.order_agreement_required? && !@order.copayer_order_agreement_done?
    @cta_count+=1
end

if @cta_count > 0
end

end

First thing to try: make your last line just @cta_count > 0. You don’t need the if.

Also, if you don’t need to reference @cta_count is other methods, just use a local variable (just remove the @-sign).

Excellent!! Yup, that worked. Big Thanks!!

It’s also worth noting that, if you don’t actually need the @cta_count, this whole thing collapses to

def signing_ctas?
  !@order.already_a_club_member || ( 
    @order.copayed? && 
    @order.order_agreement_required? && !@order.copayer_order_agreement_done?
  )
end

I would definitely break that large boolean into smaller, well-named parts though.

The repeated use of @order also hints at some feature envy – maybe this logic should live on Order?

1 Like

What @pat said. :smile:

Yes, Pat. These big blocks of code are very un-ruby like that I created. It turns out that the whole logic will be changed to make it DRY by a senior developer.

Good catch! You saw this problem right away.

Thanks!