← Back to Upcase

How to make a method return true or false?


(Kim Khan) #1

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


(Ben Orenstein) #2

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).


(Kim Khan) #3

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


(pat brisbin) #4

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?


(Ben Orenstein) #5

What @pat said. :smile:


(Kim Khan) #6

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!