Combination of builtin Enumerable#inject and splat operator behaves wrong
-------------------------------------------------------------------------

                 Key: JRUBY-2162
                 URL: http://jira.codehaus.org/browse/JRUBY-2162
             Project: JRuby
          Issue Type: Bug
          Components: Compiler, Core Classes/Modules, Interpreter
    Affects Versions: JRuby 1.1RC2
            Reporter: Stefan Lang


Short sample: jruby -e '[1].inject(0) { |*args| p args }'
prints: [[0, 1]]
Args are wrapped in an additional array.

More example code: save the follwing code in splat_and_inject.rb:

    puts "my_inject"

    module Enumerable
      def my_inject(init)
        acc = init
        each { |elem| acc = yield acc, elem }
        acc
      end
    end

    # OK
    p [1, 2].my_inject(0) { |a, b|
      puts "a: #{a.inspect}, b: #{b.inspect}"
      a + b
    }

    # broken, jruby wraps args in an additional array
    p [1, 2].my_inject(0) { |*args|
      puts "args: #{args.inspect}"
      args[0] + args[1]
    }

    puts "builtin inject"

    # OK
    p [1, 2].inject(0) { |a, b|
      puts "a: #{a.inspect}, b: #{b.inspect}"
      a + b
    }

    # broken, jruby wraps args in an additional array
    p [1, 2].inject(0) { |*args|
      puts "args: #{args.inspect}"
      args[0] + args[1]
    }

Output when run with ruby 1.8.6:
    $ ruby186 splat_and_inject.rb
    my_inject
    a: 0, b: 1
    a: 1, b: 2
    3
    args: [0, 1]
    args: [1, 2]
    3
    builtin inject
    a: 0, b: 1
    a: 1, b: 2
    3
    args: [0, 1]
    args: [1, 2]
    3

and with jruby 1.1RC2:
    $ jruby splat_and_inject.rb
    my_inject
    a: 0, b: 1
    a: 1, b: 2
    3
    args: [0, 1]
    args: [1, 2]
    3
    builtin inject
    a: 0, b: 1
    a: 1, b: 2
    3
    args: [[0, 1]]
    splat_and_inject.rb:32: can't convert nil into Array (TypeError)
            from splat_and_inject.rb:32:in `inject'
            from splat_and_inject.rb:32:in `each'
            from splat_and_inject.rb:32:in `inject'
            from splat_and_inject.rb:32

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://jira.codehaus.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to