Getting (var = false) to return true?

Discussion in 'Ruby' started by Joe Ruby MUDCRAP-CE, Oct 27, 2006.

  1. In Rails (it's more of a Ruby question though ;) ), I have:

    def before_update
    @var = get_old_var_value
    end

    and if get_old_var_value returns a false value it causes the update to
    cancel. Is there some function that'll return true after setting the
    var? Something like, oh I dunno, set_var:)var, get_old_var_value).
    Rails' write_attribute also seems to return the value of the var (so
    false causes problems with it too). I know I can just simply do this:

    def before_update
    @var = get_old_var_value
    true
    end

    But I'm just curious if there are other approaches.

    Thanks,
    Joe

    --
    Posted via http://www.ruby-forum.com/.
    Joe Ruby MUDCRAP-CE, Oct 27, 2006
    #1
    1. Advertising

  2. On 27/10/06, Joe Ruby MUDCRAP-CE <> wrote:
    > In Rails (it's more of a Ruby question though ;) ), I have:
    >
    > def before_update
    > @var = get_old_var_value
    > end
    >
    > and if get_old_var_value returns a false value it causes the update to
    > cancel. Is there some function that'll return true after setting the
    > var? Something like, oh I dunno, set_var:)var, get_old_var_value).
    > Rails' write_attribute also seems to return the value of the var (so
    > false causes problems with it too). I know I can just simply do this:
    >
    > def before_update
    > @var = get_old_var_value
    > true
    > end
    >
    > But I'm just curious if there are other approaches.
    >
    > Thanks,
    > Joe
    >
    > --
    > Posted via http://www.ruby-forum.com/.


    def before_update
    @var = get_old_var_value || true
    end

    This returns get_old_var_value if it's not false (or nil) or true
    otherwise. Is that what you need?

    Farrel
    Farrel Lifson, Oct 27, 2006
    #2
    1. Advertising

  3. On 27/10/06, Farrel Lifson <> wrote:
    > On 27/10/06, Joe Ruby MUDCRAP-CE <> wrote:
    > > In Rails (it's more of a Ruby question though ;) ), I have:
    > >
    > > def before_update
    > > @var = get_old_var_value
    > > end
    > >
    > > and if get_old_var_value returns a false value it causes the update to
    > > cancel. Is there some function that'll return true after setting the
    > > var? Something like, oh I dunno, set_var:)var, get_old_var_value).
    > > Rails' write_attribute also seems to return the value of the var (so
    > > false causes problems with it too). I know I can just simply do this:
    > >
    > > def before_update
    > > @var = get_old_var_value
    > > true
    > > end
    > >
    > > But I'm just curious if there are other approaches.
    > >
    > > Thanks,
    > > Joe
    > >
    > > --
    > > Posted via http://www.ruby-forum.com/.

    >
    > def before_update
    > @var = get_old_var_value || true
    > end
    >
    > This returns get_old_var_value if it's not false (or nil) or true
    > otherwise. Is that what you need?
    >
    > Farrel
    >
    >


    Whoops there's a syntax error in my solution. You want
    (@var = get_old_var_value) || true
    otherwise you risk setting @var to true when get_old_var is false.

    Farrel
    Farrel Lifson, Oct 27, 2006
    #3
  4. Farrel Lifson wrote:
    > On 27/10/06, Joe Ruby MUDCRAP-CE <> wrote:
    >> false causes problems with it too). I know I can just simply do this:
    >>
    >> --
    >> Posted via http://www.ruby-forum.com/.

    >
    > def before_update
    > @var = get_old_var_value || true
    > end
    >
    > This returns get_old_var_value if it's not false (or nil) or true
    > otherwise. Is that what you need?
    >
    > Farrel


    Nah, I need the value returned from the function to always be true. Hmm,
    maybe this will work:

    def before_update
    (@var = get_old_var_value) || true
    end

    Joe

    --
    Posted via http://www.ruby-forum.com/.
    Joe Ruby MUDCRAP-CE, Oct 27, 2006
    #4
  5. > Nah, I need the value returned from the function to always be true. Hmm,
    > maybe this will work:


    Ruby considers any object that is not false or nil to be true.
    Farrel Lifson, Oct 27, 2006
    #5
  6. Farrel Lifson wrote:
    >> Nah, I need the value returned from the function to always be true. Hmm,
    >> maybe this will work:

    >
    > Ruby considers any object that is not false or nil to be true.


    Yup.

    --
    Posted via http://www.ruby-forum.com/.
    Joe Ruby MUDCRAP-CE, Oct 27, 2006
    #6
  7. Joe Ruby MUDCRAP-CE wrote:
    > (@var = get_old_var_value) || true

    Err... why is that better than
    @var = get_old_var_value; true
    ?

    Devin
    Devin Mullins, Oct 27, 2006
    #7
  8. Joe Ruby MUDCRAP-CE wrote:
    > Farrel Lifson wrote:
    >> On 27/10/06, Joe Ruby MUDCRAP-CE <> wrote:
    >>> false causes problems with it too). I know I can just simply do this:
    >>>
    >>> --
    >>> Posted via http://www.ruby-forum.com/.

    >> def before_update
    >> @var = get_old_var_value || true
    >> end
    >>
    >> This returns get_old_var_value if it's not false (or nil) or true
    >> otherwise. Is that what you need?
    >>
    >> Farrel

    >
    > Nah, I need the value returned from the function to always be true. Hmm,
    > maybe this will work:
    >
    > def before_update
    > (@var = get_old_var_value) || true
    > end


    In that case the code is too complicated. Rather use

    def before_update
    @var = get_old_var_value
    true
    end

    There is no point in having "||" or "or" here.

    robert
    Robert Klemme, Oct 27, 2006
    #8
    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. Siemel Naran

    Does true ^ true return false?

    Siemel Naran, Jun 17, 2004, in forum: C++
    Replies:
    19
    Views:
    652
    Chris Theis
    Jun 18, 2004
  2. André
    Replies:
    3
    Views:
    1,563
  3. bdb112
    Replies:
    45
    Views:
    1,321
    jazbees
    Apr 29, 2009
  4. Arie Kusuma Atmaja
    Replies:
    6
    Views:
    134
    Arie Kusuma Atmaja
    Apr 11, 2008
  5. w i l l
    Replies:
    4
    Views:
    230
    Dan Brussee
    Jul 4, 2003
Loading...

Share This Page