Ruby bug in +=?

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”]!

On May 31, 10:40 pm, “[email protected][email protected]
wrote:

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

Rewrite this longhand as:

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

Which really means:

$list = [] + [“d”]

On Jun 1, 3:00 am, [email protected] 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.

On 6/1/07, [email protected] [email protected] 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/

Hi –

On Fri, 1 Jun 2007, [email protected] 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 :slight_smile:

David

[email protected] 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.

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