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

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

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

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.

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.