Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Optionally pass a parameter? (http://www.velocityreviews.com/forums/t853236-optionally-pass-a-parameter.html)

Peter Alvin 09-23-2008 11:50 AM

Optionally pass a parameter?
 
In general, how do you optionally pass an optional qualifier to a Ruby
def?

For example, for some model in Rails, like this:

MyDBClass.find(:all, ...

How do I optionally add a condition? This generates a syntax error:

some_test ? :conditions => "id=100" : nil,

I guess it doesn't like that I only want to add the :conditions
occasionally. What is the Ruby best practice or idiom for this?

Pete
--
Posted via http://www.ruby-forum.com/.


James Coglan 09-23-2008 11:57 AM

Re: Optionally pass a parameter?
 
[Note: parts of this message were removed to make it a legal post.]

>
> some_test ? :conditions => "id=100" : nil



Try turning it inside out:

:conditions => (some_test ? "id=100" : nil)

or:

:conditions => (some_test && "id=100")

Not too sure about what the second one will cause Rails to do if some_test
is false rather than nil, but worth a try.



All times are GMT. The time now is 10:26 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.