# Getting (var = false) to return true?

**URL:** <https://www.ruby-forum.com/t/getting-var-false-to-return-true/73817>\
**Category:** Ruby\
**Created:** [October 27, 2006, 9:59am UTC](https://www.ruby-forum.com/t/getting-var-false-to-return-true/73817 "2006-10-27T09:59:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 27, 2006, 9:59am UTC](https://www.ruby-forum.com/t/getting-var-false-to-return-true/73817/1 "2006-10-27T09:59:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 27, 2006, 10:13am UTC](https://www.ruby-forum.com/t/getting-var-false-to-return-true/73817/2 "2006-10-27T10:13:14Z")

</div>

On 27/10/06, Joe R. MUDCRAP-CE [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> false causes problems with it too). I know I can just simply do this:
> 
> –  
> Posted via [http://www.ruby-forum.com/](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

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 27, 2006, 10:19am UTC](https://www.ruby-forum.com/t/getting-var-false-to-return-true/73817/3 "2006-10-27T10:19:07Z")

</div>

Farrel L. wrote:

> On 27/10/06, Joe R. MUDCRAP-CE [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > false causes problems with it too). I know I can just simply do this:
> > 
> > –  
> > Posted via [http://www.ruby-forum.com/](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

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 27, 2006, 10:19am UTC](https://www.ruby-forum.com/t/getting-var-false-to-return-true/73817/4 "2006-10-27T10:19:14Z")

</div>

On 27/10/06, Farrel L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Rails’ write\_attribute also seems to return the value of the var (so  
> > Joe
> 
> 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

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 27, 2006, 10:47am UTC](https://www.ruby-forum.com/t/getting-var-false-to-return-true/73817/5 "2006-10-27T10:47:28Z")

</div>

Farrel L. 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.

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 27, 2006, 10:27am UTC](https://www.ruby-forum.com/t/getting-var-false-to-return-true/73817/6 "2006-10-27T10:27:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 27, 2006, 1:35pm UTC](https://www.ruby-forum.com/t/getting-var-false-to-return-true/73817/7 "2006-10-27T13:35:28Z")

</div>

Joe R. MUDCRAP-CE wrote:

> (@var = get\_old\_var\_value) || true  
> Err… why is that better than  
> @var = get\_old\_var\_value; true  
> ?

Devin

---

<div class="post-metadata">

**Author:** ![joe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b9e5f3/32.png) [@joe](https://www.ruby-forum.com/u/joe)\
**Post date:** [October 27, 2006, 7:06pm UTC](https://www.ruby-forum.com/t/getting-var-false-to-return-true/73817/8 "2006-10-27T19:06:04Z")

</div>

Joe R. MUDCRAP-CE wrote:

> > 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
```
