# Setting variable with either value

**URL:** <https://www.ruby-forum.com/t/setting-variable-with-either-value/197534>\
**Category:** Ruby\
**Created:** [October 15, 2010, 9:07pm UTC](https://www.ruby-forum.com/t/setting-variable-with-either-value/197534 "2010-10-15T21:07:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![okkezSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/85f322/32.png) [@okkezSS](https://www.ruby-forum.com/u/okkezSS)\
**Post date:** [October 15, 2010, 9:07pm UTC](https://www.ruby-forum.com/t/setting-variable-with-either-value/197534/1 "2010-10-15T21:07:33Z")

</div>

Is there a better way to write this?

@a = “abc”

@a = @b if @b

This will set value @a to @b if @b already have a value since before. If  
not it will use the default @a which is “abc”.

---

<div class="post-metadata">

**Author:** ![paulbutcher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/a5b964/32.png) [@paulbutcher](https://www.ruby-forum.com/u/paulbutcher)\
**Post date:** [October 15, 2010, 10:39pm UTC](https://www.ruby-forum.com/t/setting-variable-with-either-value/197534/2 "2010-10-15T22:39:50Z")

</div>

On Oct 15, 2010, at 3:07 PM, Paul B. wrote:

> Is there a better way to write this?
> 
> @a = “abc”
> 
> @a = @b if @b
> 
> This will set value @a to @b if @b already have a value since  
> before. If  
> not it will use the default @a which is “abc”.

If I understand you, this will do that:

@a = @b || “abc”

-Rob

Rob B.  
removed\_email\_address@domain.invalid [http://AgileConsultingLLC.com/](http://AgileConsultingLLC.com/)  
removed\_email\_address@domain.invalid [http://GaslightSoftware.com/](http://GaslightSoftware.com/)
