Paul Jonathan Thompson
2010-May-18 06:41 UTC
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Frederick Cheung
2010-May-18 07:22 UTC
Re: Newby Ruby question, how to I test to know if a method that returns true or false has done so?
On May 18, 7:41 am, Paul Jonathan Thompson <rails...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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> Thanks in advance. > > Paul Thompson > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2010-May-18 07:33 UTC
Re: Newby Ruby question, how to I test to know if a method that returns true or false has done so?
On 18 May 2010 07:41, Paul Jonathan Thompson <rails001-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Paul Jonathan Thompson
2010-May-18 07:42 UTC
Re: Newby Ruby question, how to I test to know if a method that returns true or false has done so?
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, On 18 May 2010 19:33, Michael Pavling <pavling-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 18 May 2010 07:41, Paul Jonathan Thompson <rails001-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. > > -- > You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Michael Pavling
2010-May-18 07:44 UTC
Re: Newby Ruby question, how to I test to know if a method that returns true or false has done so?
On 18 May 2010 08:42, Paul Jonathan Thompson <rails001-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.