# Ruby bug in +=?

**URL:** <https://www.ruby-forum.com/t/ruby-bug-in/105671>\
**Category:** Ruby\
**Created:** [June 1, 2007, 7:45am UTC](https://www.ruby-forum.com/t/ruby-bug-in/105671 "2007-06-01T07:45:54Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 1, 2007, 7:45am UTC](https://www.ruby-forum.com/t/ruby-bug-in/105671/1 "2007-06-01T07:45:54Z")

</div>

def addToList(x)  
$list += [x];  
end

def function()  
addToList(“a”);  
addToList(“b”);  
addToList(“c”);  
return “d”;  
end

$list = [];  
addToList(function()); # $list = [“a”,“b”,“c”,“d”]

$list = [];  
$list += [function()]; # $list = [“d”]!

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 1, 2007, 8:02am UTC](https://www.ruby-forum.com/t/ruby-bug-in/105671/2 "2007-06-01T08:02:56Z")

</div>

On May 31, 10:40 pm, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> $list = [];  
> $list += [function()]; # $list = [“d”]!

Rewrite this longhand as:

$list = [];  
$list = $list + [function()];

Which really means:

$list = [] + [“d”]

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 1, 2007, 8:26am UTC](https://www.ruby-forum.com/t/ruby-bug-in/105671/3 "2007-06-01T08:26:59Z")

</div>

On Jun 1, 3:00 am, removed\_email\_address@domain.invalid wrote:

> Rewrite this longhand as:
> 
> $list = [];  
> $list = $list + [function()];
> 
> Which really means:
> 
> $list = [] + [“d”]

Yeah, figured. It’s just odd, and allows for some nice obfuscated  
coding.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 1, 2007, 9:03am UTC](https://www.ruby-forum.com/t/ruby-bug-in/105671/4 "2007-06-01T09:03:01Z")

</div>

On 6/1/07, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Yeah, figured. It’s just odd, and allows for some nice obfuscated  
> coding.

Are you expecting [“a”,“b”,“c”,“d”] ?  
Something to look at…

$list = $list + [function()]; # [“d”]  
but  
$list = [function()] + $list; # [“d”,“a”,“b”,“c”]

## Harry

A Look into Japanese Ruby List in English  
[http://www.kakueki.com/](http://www.kakueki.com/)

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 1, 2007, 11:53am UTC](https://www.ruby-forum.com/t/ruby-bug-in/105671/5 "2007-06-01T11:53:38Z")

</div>

Hi –

On Fri, 1 Jun 2007, removed\_email\_address@domain.invalid wrote:

> Yeah, figured. It’s just odd, and allows for some nice obfuscated  
> coding.

I’m not sure how you’d obfuscate anything using +=. As far as I know,  
it’s completely consistent in the way it’s parsed, and thoroughly  
documented. Once you know how it works, the obfuscation mysteriously  
vanishes 🙂

David

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 1, 2007, 9:21am UTC](https://www.ruby-forum.com/t/ruby-bug-in/105671/6 "2007-06-01T09:21:13Z")

</div>

removed\_email\_address@domain.invalid wrote:

> Yeah, figured. It’s just odd, and allows for some nice obfuscated  
> coding.

I’d suggest what’s making for the obfuscated code here is not +=, but  
the use of a global variable  
and a function that has side effects.

The behavior of += doesn’t seem odd to me. I’d say it’s well defined.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 1, 2007, 1:30pm UTC](https://www.ruby-forum.com/t/ruby-bug-in/105671/7 "2007-06-01T13:30:50Z")

</div>

On 01.06.2007 09:15, Michael H. wrote:

> > Yeah, figured. It’s just odd, and allows for some nice obfuscated  
> > coding.
> 
> I’d suggest what’s making for the obfuscated code here is not +=, but  
> the use of a global variable and a function that has side effects.
> 
> The behavior of += doesn’t seem odd to me. I’d say it’s well defined.

Absolutely agree! The code presented is pretty weird and far from  
straight forward IMHO.

Kind regards

robert
