# Idiomatic way to assign if not nil?

**URL:** <https://rubytalk.org/t/idiomatic-way-to-assign-if-not-nil/40240>\
**Category:** ruby-talk\
**Created:** [29 August 2007 13:40 UTC](https://rubytalk.org/t/idiomatic-way-to-assign-if-not-nil/40240 "2007-08-29T13:40:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ken\_Bloom](https://avatars.discourse-cdn.com/v4/letter/k/f08c70/32.png) [@Ken\_Bloom](https://rubytalk.org/u/Ken_Bloom)\
**Post date:** [29 August 2007 13:40 UTC](https://rubytalk.org/t/idiomatic-way-to-assign-if-not-nil/40240/1 "2007-08-29T13:40:04Z")

</div>

But a is defintiely a variable, while b may be a function call.  
In a = b if b, you compute the function twice.  
In a = b or a, you compute the function only once.

--Ken

> **···**
>
> On Wed, 29 Aug 2007 08:53:19 +0900, Logan Capaldo wrote:
> 
> > On 8/28/07, Douglas F Shearer \<dougal.s@gmail.com\> wrote:
> > 
> > > Hi Jay
> > > 
> > > On 29 Aug 2007, at 00:30, Jay Levitt wrote:
> > > 
> > > \> a = b if b  
> > > \>  
> > > \> Does that exist in a DRYer form?
> > > 
> > > Best I could come up with just now was:
> > > 
> > > a = b || a
> > 
> > &nbsp;&nbsp;&nbsp;a = b if b  
> > Not really DRYer :), you just repeat a instead of b 🙂
> 
> --  
> Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.  
> Department of Computer Science. Illinois Institute of Technology.  
> [http://www.iit.edu/~kbloom1/](http://www.iit.edu/~kbloom1/)

---

<div class="post-metadata">

**Author:** ![Douglas\_F\_Shearer](https://avatars.discourse-cdn.com/v4/letter/d/59ef9b/32.png) [@Douglas\_F\_Shearer](https://rubytalk.org/u/Douglas_F_Shearer)\
**Post date:** [29 August 2007 21:44 UTC](https://rubytalk.org/t/idiomatic-way-to-assign-if-not-nil/40240/2 "2007-08-29T21:44:28Z")

</div>

Took the words right out of my mouth, I must read my email more frequently...

Douglas F Shearer  
dougal.s@gmail.com

> **[Douglas F Shearer](https://douglasfshearer.com/)**
>
> Software developer, mountainbiker.

> **···**
>
> On 29 Aug 2007, at 14:40, Ken Bloom wrote:
> 
> > On Wed, 29 Aug 2007 08:53:19 +0900, Logan Capaldo wrote:
> > 
> > > On 8/28/07, Douglas F Shearer \<dougal.s@gmail.com\> wrote:
> > > 
> > > > Hi Jay
> > > > 
> > > > On 29 Aug 2007, at 00:30, Jay Levitt wrote:
> > > > 
> > > > > a = b if b
> > > > > 
> > > > > Does that exist in a DRYer form?
> > > > 
> > > > Best I could come up with just now was:
> > > > 
> > > > a = b || a
> > > 
> > > &nbsp;&nbsp;&nbsp;a = b if b  
> > > Not really DRYer :), you just repeat a instead of b 🙂
> > 
> > But a is defintiely a variable, while b may be a function call.  
> > In a = b if b, you compute the function twice.  
> > In a = b or a, you compute the function only once.

---

<div class="post-metadata">

**Author:** ![Logan\_Capaldo](https://avatars.discourse-cdn.com/v4/letter/l/7ea924/32.png) [@Logan\_Capaldo](https://rubytalk.org/u/Logan_Capaldo)\
**Post date:** [29 August 2007 22:38 UTC](https://rubytalk.org/t/idiomatic-way-to-assign-if-not-nil/40240/3 "2007-08-29T22:38:31Z")

</div>

> \>\> Hi Jay  
> \>\>  
> \>\>  
> \>\> \> a = b if b  
> \>\> \>  
> \>\> \> Does that exist in a DRYer form?  
> \>\>  
> \>\> Best I could come up with just now was:  
> \>\>  
> \>\> a = b || a  
> \> a = b if b  
> \> Not really DRYer :), you just repeat a instead of b 🙂
> 
> But a is defintiely a variable, while b may be a function call.  
> In a = b if b, you compute the function twice.  
> In a = b or a, you compute the function only once.

Methods that are expensive and/or have side effects should probably  
not be named b 🙂

> **···**
>
> On 8/29/07, Ken Bloom \<kbloom@gmail.com\> wrote:
> 
> > On Wed, 29 Aug 2007 08:53:19 +0900, Logan Capaldo wrote:  
> > \> On 8/28/07, Douglas F Shearer \<dougal.s@gmail.com\> wrote:  
> > \>\> On 29 Aug 2007, at 00:30, Jay Levitt wrote:  
> > --Ken
> > 
> > --  
> > Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.  
> > Department of Computer Science. Illinois Institute of Technology.  
> > [http://www.iit.edu/~kbloom1/](http://www.iit.edu/~kbloom1/)
