# Make an exmpersion simpler

**URL:** <https://www.ruby-forum.com/t/make-an-exmpersion-simpler/179212>\
**Category:** Ruby\
**Created:** [November 19, 2009, 7:27pm UTC](https://www.ruby-forum.com/t/make-an-exmpersion-simpler/179212 "2009-11-19T19:27:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![benol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@benol](https://www.ruby-forum.com/u/benol)\
**Post date:** [November 19, 2009, 7:27pm UTC](https://www.ruby-forum.com/t/make-an-exmpersion-simpler/179212/1 "2009-11-19T19:27:20Z")

</div>

Any way to make this shorter?

list2 \<\< list1 if list1 && !list1.empty?

Thanks.

---

<div class="post-metadata">

**Author:** ![benol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@benol](https://www.ruby-forum.com/u/benol)\
**Post date:** [November 19, 2009, 7:46pm UTC](https://www.ruby-forum.com/t/make-an-exmpersion-simpler/179212/2 "2009-11-19T19:46:54Z")

</div>

Michal B. wrote:

> Any way to make this shorter?
> 
> list2 \<\< list1 if list1 && !list1.empty?
> 
> Thanks.

You want to add a list as an element to another list?  
Just making sure we’re talking about the same thing.

---

<div class="post-metadata">

**Author:** ![benol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@benol](https://www.ruby-forum.com/u/benol)\
**Post date:** [November 19, 2009, 10:15pm UTC](https://www.ruby-forum.com/t/make-an-exmpersion-simpler/179212/3 "2009-11-19T22:15:58Z")

</div>

Michal B. wrote:

> Any way to make this shorter?
> 
> list2 \<\< list1 if list1 && !list1.empty?
> 
> Thanks.

if you know list1 will never be nil, then you can do

list2 \<\< list1 unless list1.empty?

~Jeremy

---

<div class="post-metadata">

**Author:** ![benol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@benol](https://www.ruby-forum.com/u/benol)\
**Post date:** [November 20, 2009, 10:51am UTC](https://www.ruby-forum.com/t/make-an-exmpersion-simpler/179212/4 "2009-11-20T10:51:53Z")

</div>

list1 is a list  
list1 can be nil  
i wan add all elements from list1 to list2

---

<div class="post-metadata">

**Author:** ![benol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@benol](https://www.ruby-forum.com/u/benol)\
**Post date:** [November 20, 2009, 11:36am UTC](https://www.ruby-forum.com/t/make-an-exmpersion-simpler/179212/5 "2009-11-20T11:36:53Z")

</div>

Michal B. wrote:

> i wan add all elements from list1 to list2

That is not what your code does.

This should work:

list2.concat(list1 || [])

Tor Erik  
[http://tel.jklm.no/](http://tel.jklm.no/)

---

<div class="post-metadata">

**Author:** ![benol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@benol](https://www.ruby-forum.com/u/benol)\
**Post date:** [November 20, 2009, 2:51pm UTC](https://www.ruby-forum.com/t/make-an-exmpersion-simpler/179212/6 "2009-11-20T14:51:26Z")

</div>

2009/11/20 Tor Erik L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> Michal B. wrote:
> 
> > i wan add all elements from list1 to list2
> 
> That is not what your code does.
> 
> This should work:
> 
> list2.concat(list1 || [])

Here’s a variant which will avoid the creation of the empty array:

list1 and list2.concat list1

I would not explicitly check for empty list. Array#concat will handle  
that efficiently in C code.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![benol](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bb73d2/32.png) [@benol](https://www.ruby-forum.com/u/benol)\
**Post date:** [November 20, 2009, 2:07pm UTC](https://www.ruby-forum.com/t/make-an-exmpersion-simpler/179212/7 "2009-11-20T14:07:16Z")

</div>

Tor Erik L. wrote:

> Michal B. wrote:
> 
> > i wan add all elements from list1 to list2
> 
> That is not what your code does.
> 
> This should work:
> 
> list2.concat(list1 || [])

If, however, we know that list1 _does exist_ as a variable, then

> > list2.concat list1  
> > is all we need. If list1 does not exist, your code will return an error,  
> > and so…
> 
> > list2.concat list1 rescue nil  
> > This will concatenate list1 and list2, unless list1 does not exist, in  
> > which case it’ll return nil instead of an error, and list2 will remain  
> > the same.
