Optionally pass a parameter?

Discussion in 'Ruby' started by Peter Alvin, Sep 23, 2008.

  1. Peter Alvin

    Peter Alvin Guest

    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/.
     
    Peter Alvin, Sep 23, 2008
    #1
    1. Advertising

  2. Peter Alvin

    James Coglan Guest

    [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.
     
    James Coglan, Sep 23, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. eagle

    How to pass a control optionally

    eagle, Sep 2, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    345
    Karl Seguin
    Sep 2, 2005
  2. Sam Iam
    Replies:
    0
    Views:
    475
    Sam Iam
    Jan 31, 2004
  3. Kirill Simonov
    Replies:
    2
    Views:
    325
    =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?=
    Oct 9, 2006
  4. Dilip
    Replies:
    5
    Views:
    411
  5. Mark Space
    Replies:
    3
    Views:
    639
    Daniel Pitts
    Sep 30, 2008
Loading...

Share This Page