# ||= for arrays

**URL:** <https://www.ruby-forum.com/t/for-arrays/70668>\
**Category:** Rails\
**Created:** [October 1, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/for-arrays/70668 "2006-10-01T19:21:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sebastian\_Probst\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Sebastian\_Probst\_E](https://www.ruby-forum.com/u/Sebastian_Probst_E)\
**Post date:** [October 1, 2006, 7:21pm UTC](https://www.ruby-forum.com/t/for-arrays/70668/1 "2006-10-01T19:21:19Z")

</div>

Dear messageboard users  
I am just wandering if there is any operator similar to the ||= for  
arrays?

#DOES NOT WORK  
test\_array = []  
test\_array ||= [‘nothing…’]

#DOES NOT WORK EITHER  
test\_array = []  
test\_array ||\<\< [‘nothing…’]

You could write something like this:  
test\_array \<\< [‘nothing…’] if test\_array.size == 0

But that just doesn’t look half as good as any of the two above in my  
opinion.

Best regards  
Sebastian Probst E.

---

<div class="post-metadata">

**Author:** ![Sebastian\_Probst\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Sebastian\_Probst\_E](https://www.ruby-forum.com/u/Sebastian_Probst_E)\
**Post date:** [October 1, 2006, 7:37pm UTC](https://www.ruby-forum.com/t/for-arrays/70668/2 "2006-10-01T19:37:23Z")

</div>

On 10/1/06, Sebastian Probst E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> test\_array = []  
> test\_array ||\<\< [‘nothing…’]
> 
> You could write something like this:  
> test\_array \<\< [‘nothing…’] if test\_array.size == 0
> 
> But that just doesn’t look half as good as any of the two above in my  
> opinion.

The ||= operator checks that the variable in question evaluates to nil  
(or false, I think).

So, its behaving exactly as expected, since an empty array is not nil or  
false.

-ryan

---

<div class="post-metadata">

**Author:** ![Sebastian\_Probst\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Sebastian\_Probst\_E](https://www.ruby-forum.com/u/Sebastian_Probst_E)\
**Post date:** [October 1, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/for-arrays/70668/3 "2006-10-01T22:41:38Z")

</div>

On Sunday 01 October 2006 19:17, Sebastian Probst E. wrote:

> Dear messageboard users  
> I am just wandering if there is any operator similar to the ||= for  
> arrays?
> 
> #DOES NOT WORK  
> test\_array = []  
> test\_array ||= [‘nothing…’]

test\_array |= [‘nothing…’]

Array#| is the union operator.

Michael

–  
Michael S.  
[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://www.schuerig.de/michael/](http://www.schuerig.de/michael/)

---

<div class="post-metadata">

**Author:** ![Sebastian\_Probst\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Sebastian\_Probst\_E](https://www.ruby-forum.com/u/Sebastian_Probst_E)\
**Post date:** [October 1, 2006, 11:45pm UTC](https://www.ruby-forum.com/t/for-arrays/70668/4 "2006-10-01T23:45:41Z")

</div>

Hi –

On Sun, 1 Oct 2006, Michael S. wrote:

> test\_array |= [‘nothing…’]
> 
> Array#| is the union operator.

The problem with that (at least potentially, depending on what else is  
happening) is that it creates a new array object, rather than  
modifying test\_array in place.

David

–  
David A. Black | removed\_email\_address@domain.invalid  
Author of “Ruby for Rails” [1] | Ruby/Rails training & consultancy [3]  
DABlog (DAB’s Weblog) [2] | Co-director, Ruby Central, Inc. [4]  
[1] [Ruby for Rails](http://www.manning.com/black) | [3] [http://www.rubypowerandlight.com](http://www.rubypowerandlight.com)  
[2] [http://dablog.rubypal.com](http://dablog.rubypal.com) | [4] [http://www.rubycentral.org](http://www.rubycentral.org)

---

<div class="post-metadata">

**Author:** ![Sebastian\_Probst\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Sebastian\_Probst\_E](https://www.ruby-forum.com/u/Sebastian_Probst_E)\
**Post date:** [October 1, 2006, 10:05pm UTC](https://www.ruby-forum.com/t/for-arrays/70668/5 "2006-10-01T22:05:35Z")

</div>

2006/10/1, Sebastian Probst E. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> You could write something like this:  
> test\_array \<\< [‘nothing…’] if test\_array.size == 0

You could do:

> > array = []  
> > =\> []  
> > array \<\< ‘nothing’ if array.empty?  
> > =\> [“nothing”]  
> > array \<\< ‘something’ if array.empty?  
> > =\> nil  
> > array  
> > =\> [“nothing”]

---

<div class="post-metadata">

**Author:** ![Sebastian\_Probst\_E](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/90db22/32.png) [@Sebastian\_Probst\_E](https://www.ruby-forum.com/u/Sebastian_Probst_E)\
**Post date:** [October 2, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/for-arrays/70668/6 "2006-10-02T18:57:30Z")

</div>

Thanks for all the answers!

Best regards  
Sebastian
