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

Rewrite this longhand as:

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

Which really means:

$list = + ["d"]

···

On May 31, 10:40 pm, "zarawes...@gmail.com" <zarawes...@gmail.com> wrote:

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

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

···

On Jun 1, 3:00 am, james.d.mast...@gmail.com wrote:

Rewrite this longhand as:

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

Which really means:

$list = + ["d"]

Are you expecting ["a","b","c","d"] ?
Something to look at....

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

Harry

···

On 6/1/07, zarawesome@gmail.com <zarawesome@gmail.com> wrote:

On Jun 1, 3:00 am, james.d.mast...@gmail.com 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.

--

A Look into Japanese Ruby List in English

zarawesome@gmail.com wrote:

···

On Jun 1, 3:00 am, james.d.mast...@gmail.com 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.

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.

Hi --

···

On Fri, 1 Jun 2007, zarawesome@gmail.com wrote:

On Jun 1, 3:00 am, james.d.mast...@gmail.com 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.

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

--
Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black\)
    (See what readers are saying! http://www.rubypal.com/r4rrevs.pdf\)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)

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

Kind regards

  robert

···

On 01.06.2007 09:15, Michael Hollins wrote:

zarawesome@gmail.com wrote:

On Jun 1, 3:00 am, james.d.mast...@gmail.com 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.

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.