Getting (var = false) to return true?

  • Thread starter Joe Ruby MUDCRAP-CE
  • Start date
J

Joe Ruby MUDCRAP-CE

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
 
F

Farrel Lifson

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

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
 
F

Farrel Lifson

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
 
J

Joe Ruby MUDCRAP-CE

Farrel said:
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
 
F

Farrel Lifson

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.
 
R

Robert Klemme

Joe said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top