Ruby bug in +=?

Discussion in 'Ruby' started by zarawesome@gmail.com, Jun 1, 2007.

  1. Guest

    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"]!
     
    , Jun 1, 2007
    #1
    1. Advertising

  2. Guest

    On May 31, 10:40 pm, "" <>
    wrote:
    > $list = [];
    > $list += [function()]; # $list = ["d"]!


    Rewrite this longhand as:

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

    Which really means:

    $list = [] + ["d"]
     
    , Jun 1, 2007
    #2
    1. Advertising

  3. Guest

    On Jun 1, 3:00 am, 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.
     
    , Jun 1, 2007
    #3
  4. On 6/1/07, <> wrote:
    > On Jun 1, 3:00 am, 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.
    >
    >
    >
    >

    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/
     
    Harry Kakueki, Jun 1, 2007
    #4
  5. Guest

    Hi --

    On Fri, 1 Jun 2007, wrote:

    > On Jun 1, 3:00 am, 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 :)


    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)
     
    , Jun 1, 2007
    #5
  6. On 01.06.2007 09:15, Michael Hollins wrote:
    > wrote:
    >> On Jun 1, 3:00 am, 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.


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

    Kind regards

    robert
     
    Robert Klemme, Jun 1, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Han Holl
    Replies:
    2
    Views:
    111
    Yukihiro Matsumoto
    Sep 1, 2003
  2. Trevor Wennblom
    Replies:
    13
    Views:
    228
    Esteban Manchado Velázquez
    Jan 16, 2005
  3. anne001
    Replies:
    1
    Views:
    523
  4. ngoc
    Replies:
    3
    Views:
    97
  5. Michael Fellinger
    Replies:
    3
    Views:
    177
    Michael Fellinger
    Dec 27, 2007
Loading...

Share This Page