JRuby (please use github issues at http://bugs.jruby.org)
  1. JRuby (please use github issues at http://bugs.jruby.org)
  2. JRUBY-6632

BigDecimal.new fails when passed anything but string (Float, BigDecimal, Fixnum)

    Details

    • Type: Bug Bug
    • Status: Resolved Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: JRuby 1.6.3, JRuby 1.7.0.pre1
    • Fix Version/s: JRuby 1.7.0.RC1
    • Component/s: Standard Library
    • Labels:
      None
    • Environment:
    • Number of attachments :
      0

      Description

      Code below, According to documentation http://www.ruby-doc.org/stdlib-1.9.3/libdoc/bigdecimal/rdoc/BigDecimal.html#method-c-new should take any numeric type or string when creating a RubyBigDecimal object. However, Jruby fails on all of these. (Note: MRI seems to fail as well but for unrelated reasons I suppose https://gist.github.com/2552909).

      Issue seems to be this code below in RubyBigDecimal.java. convertToString() is the line it fails on. Error that appears is from util/TypeConverter.java which RubyBasicObject.java uses to convert methods to string using the "to_str" method.

      @JRubyMethod(name = "new", required = 1, optional = 1, meta = true)
      309 public static RubyBigDecimal newInstance(IRubyObject recv, IRubyObject[] args) {
      310 BigDecimal decimal;
      311 if (args.length == 0)

      { 312 decimal = new BigDecimal(0); 313 }

      else {
      314 String strValue = args[0].convertToString().toString();
      315 strValue = strValue.trim();
      316 if ("NaN".equals(strValue))

      { 317 return newNaN(recv.getRuntime()); 318 }

      daidoji@darkstar:~$ cat foo.rb
      require 'bigdecimal'

      a = BigDecimal(1.0)
      b = BigDecimal(1)
      c = BigDecimal(2/3)
      d = BigDecimal(BigDecimal("1"))
      daidoji@darkstar:~$ java -jar jruby/lib/jruby.jar -v
      jruby 1.7.0.dev (ruby-1.9.3-p139) (2012-04-29 3b4ea85) (Java HotSpot(TM) Client VM 1.6.0_25) [linux-i386-java]
      rubygems.rb not found; disabling gems
      daidoji@darkstar:~$ java -jar jruby/lib/jruby.jar foo.rb
      TypeError: can't convert Float into String
      new at org/jruby/ext/bigdecimal/RubyBigDecimal.java:453
      (root) at foo.rb:3

      daidoji@darkstar:~$ vim foo.rb

      Press ENTER or type command to continue
      syntax=daidoji@darkstar:~$ java -jar jruby/lib/jruby.jar foo.rb
      TypeError: can't convert Fixnum into String
      new at org/jruby/ext/bigdecimal/RubyBigDecimal.java:453
      (root) at foo.rb:4
      daidoji@darkstar:~$ vim foo.rb

      Press ENTER or type command to continue
      syntax=daidoji@darkstar:~$ java -jar jruby/lib/jruby.jar foo.rb
      TypeError: can't convert Fixnum into String
      new at org/jruby/ext/bigdecimal/RubyBigDecimal.java:453
      (root) at foo.rb:5
      daidoji@darkstar:~$ vim foo.rb

      Press ENTER or type command to continue
      syntax=daidoji@darkstar:~$ java -jar jruby/lib/jruby.jar foo.rb
      TypeError: can't convert BigDecimal into String
      new at org/jruby/ext/bigdecimal/RubyBigDecimal.java:453
      (root) at foo.rb:6
      daidoji@darkstar:~$

        Issue Links

          Activity

          Hide
          Charles Oliver Nutter added a comment -

          With your script failing, I'm not sure what it is we ought to fix. Can you try to turn this into a test case or spec that runs properly on Ruby 1.9.3? Once we have that, it should be trivial to fix the rest.

          Show
          Charles Oliver Nutter added a comment - With your script failing, I'm not sure what it is we ought to fix. Can you try to turn this into a test case or spec that runs properly on Ruby 1.9.3? Once we have that, it should be trivial to fix the rest.
          Hide
          Charles A. Lanahan added a comment -

          This spec does not run correctly on Ruby-1.9.3 because the error was present in this stable version. It has been fixed in Ruby-head though (checked just now). I will work on a spec when I get the chance. Implementing a to_str method for each Numeric object should be a trivial solution to the problem but I have not had enough time (or experience with the edge cases of the Numeric types) in order to develop a patch or specs. If I get a chance I will try to though.

          Show
          Charles A. Lanahan added a comment - This spec does not run correctly on Ruby-1.9.3 because the error was present in this stable version. It has been fixed in Ruby-head though (checked just now). I will work on a spec when I get the chance. Implementing a to_str method for each Numeric object should be a trivial solution to the problem but I have not had enough time (or experience with the edge cases of the Numeric types) in order to develop a patch or specs. If I get a chance I will try to though.
          Hide
          Jason Lunn added a comment -
          Show
          Jason Lunn added a comment - See also: http://jira.codehaus.org/browse/JRUBY-6428
          Show
          Hiro Asari added a comment - See https://github.com/jruby/jruby/commit/2c12e33f65900c973fe8d49ad1019ef6c92b252d ( JRUBY-6428 branch) for a possible fix.
          Hide
          Hiro Asari added a comment -
          Show
          Hiro Asari added a comment - Hmm. That broke some tests. http://travis-ci.org/#!/jruby/jruby/builds/1978399
          Hide
          Brad Pardee added a comment -

          The integer part of this is still broken in preview2:

          > require 'bigdecimal'
          > require 'bigdecimal/util'
          > 5.respond_to?(:to_d)
          => true
          irb(main):004:0> 5.to_d
          TypeError: can't convert Fixnum into String
          from org/jruby/ext/bigdecimal/RubyBigDecimal.java:453:in `new'

          > BigDecimal.new(0)
          TypeError: can't convert Fixnum into String
          from org/jruby/ext/bigdecimal/RubyBigDecimal.java:453:in `new'

          > BigDecimal.new(0.5)
          => #<BigDecimal:22cb1edc,'0.5E0',1(4)>

          Show
          Brad Pardee added a comment - The integer part of this is still broken in preview2: > require 'bigdecimal' > require 'bigdecimal/util' > 5.respond_to?(:to_d) => true irb(main):004:0> 5.to_d TypeError: can't convert Fixnum into String from org/jruby/ext/bigdecimal/RubyBigDecimal.java:453:in `new' > BigDecimal.new(0) TypeError: can't convert Fixnum into String from org/jruby/ext/bigdecimal/RubyBigDecimal.java:453:in `new' > BigDecimal.new(0.5) => #<BigDecimal:22cb1edc,'0.5E0',1(4)>
          Hide
          Charles Oliver Nutter added a comment -

          I committed a partial fix in 1.7pre2 for Float, but for whatever reason I didn't also add Fixnum and BigDecimal support. Testing that now.

          Show
          Charles Oliver Nutter added a comment - I committed a partial fix in 1.7pre2 for Float, but for whatever reason I didn't also add Fixnum and BigDecimal support. Testing that now.
          Hide
          Charles Oliver Nutter added a comment -

          There's definitely more to do to bring our BigDecimal completely in line with 1.9.3, but I knocked down several issues.

          commit b16f8c38a41d96761b97f83b46d10f1219e4c725
          Author: Charles Oliver Nutter <headius@headius.com>
          Date:   Wed Aug 29 12:20:16 2012 -0500
          
              Fixes for JRUBY-6632 and other BigDecimal issues.
              
              JRUBY-6632: BigDecimal.new fails when passed anything but string (Float, BigDecimal, Fixnum)
              
              Also miscellaneous fixes for missing constants, other coercion
              types, and some error handling.
          
          :100644 100644 502851e... a516940... M	src/org/jruby/ext/bigdecimal/RubyBigDecimal.java
          :100644 100644 e70ece1... e71f660... M	test/externals/ruby1.9/excludes/TestBigDecimal.rb
          :100644 100644 c114447... 50fb26e... M	test/test_big_decimal.rb
          
          Show
          Charles Oliver Nutter added a comment - There's definitely more to do to bring our BigDecimal completely in line with 1.9.3, but I knocked down several issues. commit b16f8c38a41d96761b97f83b46d10f1219e4c725 Author: Charles Oliver Nutter <headius@headius.com> Date: Wed Aug 29 12:20:16 2012 -0500 Fixes for JRUBY-6632 and other BigDecimal issues. JRUBY-6632: BigDecimal.new fails when passed anything but string (Float, BigDecimal, Fixnum) Also miscellaneous fixes for missing constants, other coercion types, and some error handling. :100644 100644 502851e... a516940... M src/org/jruby/ext/bigdecimal/RubyBigDecimal.java :100644 100644 e70ece1... e71f660... M test/externals/ruby1.9/excludes/TestBigDecimal.rb :100644 100644 c114447... 50fb26e... M test/test_big_decimal.rb

            People

            • Assignee:
              Charles Oliver Nutter
              Reporter:
              Charles A. Lanahan
            • Votes:
              1 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: