# Newby Ruby question, how to I test to know if a method that returns true or false has done so?

**URL:** <https://www.ruby-forum.com/t/newby-ruby-question-how-to-i-test-to-know-if-a-method-that-returns-true-or-false-has-done-so/188765>\
**Category:** Rails\
**Created:** [May 18, 2010, 8:43am UTC](https://www.ruby-forum.com/t/newby-ruby-question-how-to-i-test-to-know-if-a-method-that-returns-true-or-false-has-done-so/188765 "2010-05-18T08:43:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [May 18, 2010, 8:43am UTC](https://www.ruby-forum.com/t/newby-ruby-question-how-to-i-test-to-know-if-a-method-that-returns-true-or-false-has-done-so/188765/1 "2010-05-18T08:43:04Z")

</div>

I am testing for the existence of records on a database. If I use:

Mapper.exists?(product.id)

How do I test if the condition returned was true or false?

Thanks in advance.

Paul Thompson

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [May 18, 2010, 9:34am UTC](https://www.ruby-forum.com/t/newby-ruby-question-how-to-i-test-to-know-if-a-method-that-returns-true-or-false-has-done-so/188765/2 "2010-05-18T09:34:01Z")

</div>

On May 18, 7:41 am, Paul Jonathan T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am testing for the existence of records on a database. If I use:
> 
> Mapper.exists?(product.id)
> 
> How do I test if the condition returned was true or false?

I’m not entirely sure what you’re asking. Are you just looking for

if Mapper.exists?(product.id)  
…  
else  
…  
end

Fred

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [May 18, 2010, 9:35am UTC](https://www.ruby-forum.com/t/newby-ruby-question-how-to-i-test-to-know-if-a-method-that-returns-true-or-false-has-done-so/188765/3 "2010-05-18T09:35:46Z")

</div>

On 18 May 2010 07:41, Paul Jonathan T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Mapper.exists?(product.id)
> 
> How do I test if the condition returned was true or false?

Most simply, you would use “if”… either by wrapping a conditional  
block around code to run if the condition is met, or with a “guard”  
clause. If the condition evaluates as true, then the code inside the  
block (or being guarded) with run :

if Mapper.exists?(product.id)  
@mapper = Mapper.find(product.id)  
end

or  
@mapper = Mapper.find(product.id) if Mapper.exists?(product.id)

But there’s plenty of other ways of logically checking conditions:  
“unless” is equivalent to “if !”  
Ternary operators are useful  
“case” statements have their place.  
“||=” is a lovely little Ruby idiom for checking for nil values.

If you Google for “ruby conditional logic” you’ll find lots of  
beginners guides covering this material.

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [May 18, 2010, 9:43am UTC](https://www.ruby-forum.com/t/newby-ruby-question-how-to-i-test-to-know-if-a-method-that-returns-true-or-false-has-done-so/188765/4 "2010-05-18T09:43:58Z")

</div>

Thanks for all the replies. It then works just as I had thought but  
I’m not getting the results that I expect so I need to look a bit  
further. But I now know that I am using the method correctly. Thanks  
once again,

---

<div class="post-metadata">

**Author:** ![ptremblett](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/dc4da7/32.png) [@ptremblett](https://www.ruby-forum.com/u/ptremblett)\
**Post date:** [May 18, 2010, 9:46am UTC](https://www.ruby-forum.com/t/newby-ruby-question-how-to-i-test-to-know-if-a-method-that-returns-true-or-false-has-done-so/188765/5 "2010-05-18T09:46:19Z")

</div>

On 18 May 2010 08:42, Paul Jonathan T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks for all the replies. It then works just as I had thought but  
> I’m not getting the results that I expect so I need to look a bit  
> further. But I now know that I am using the method correctly. Thanks  
> once again,

If you post the problem you’re having, rather than asking for a  
qualification on one of the solutions you thought it might be, then  
you might benefit from a few more eyes on it.
