Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Getting (var = false) to return true?

Reply
Thread Tools

Getting (var = false) to return true?

 
 
Joe Ruby MUDCRAP-CE
Guest
Posts: n/a
 
      10-27-2006
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/.

 
Reply With Quote
 
 
 
 
Farrel Lifson
Guest
Posts: n/a
 
      10-27-2006
On 27/10/06, Joe Ruby MUDCRAP-CE <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Farrel Lifson
Guest
Posts: n/a
 
      10-27-2006
On 27/10/06, Farrel Lifson <(E-Mail Removed)> wrote:
> On 27/10/06, Joe Ruby MUDCRAP-CE <(E-Mail Removed)> 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

 
Reply With Quote
 
Joe Ruby MUDCRAP-CE
Guest
Posts: n/a
 
      10-27-2006
Farrel Lifson wrote:
> On 27/10/06, Joe Ruby MUDCRAP-CE <(E-Mail Removed)> 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/.

 
Reply With Quote
 
Farrel Lifson
Guest
Posts: n/a
 
      10-27-2006
> 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.

 
Reply With Quote
 
Joe Ruby MUDCRAP-CE
Guest
Posts: n/a
 
      10-27-2006
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/.

 
Reply With Quote
 
Devin Mullins
Guest
Posts: n/a
 
      10-27-2006
Joe Ruby MUDCRAP-CE wrote:
> (@var = get_old_var_value) || true

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

Devin

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-27-2006
Joe Ruby MUDCRAP-CE wrote:
> Farrel Lifson wrote:
>> On 27/10/06, Joe Ruby MUDCRAP-CE <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM
difference between return &*i and return i; Ganesh Gella C++ 4 11-12-2004 04:28 PM
getting return value from function without return statement. Seong-Kook Shin C Programming 1 06-18-2004 08:19 AM
How do I return a return-code from main? wl Java 2 03-05-2004 05:15 PM
Return a return value from Perl to Javascript PvdK Perl 0 07-24-2003 09:20 AM



Advertisments