Details

    • Type: Bug Bug
    • Status: Resolved Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: JRuby 1.7.0.pre2
    • Fix Version/s: JRuby 1.7.0.RC2
    • Component/s: Ruby 1.9.3, RubySpec
    • Labels:
      None
    • Environment:
      Mac OS X 10.7.4 Java 1.7.0_05
    • Number of attachments :
      0

      Description

      From ruby 1.9.3 documentation:

      rand(max=0) -> number

      If max is Range, returns a pseudorandom number where range.member(number) == true

      But I am getting a TypeError:

      jruby-1.7.0.preview2 :001 > rand 1..7
      TypeError: can't convert Range into Integer
      	from org/jruby/RubyKernel.java:1580:in `rand'
      	from (irb):1:in `evaluate'
      	from org/jruby/RubyKernel.java:1070:in `eval'
      	from org/jruby/RubyKernel.java:1395:in `loop'
      	from org/jruby/RubyKernel.java:1178:in `catch'
      	from org/jruby/RubyKernel.java:1178:in `catch'
      	from /Users/tim/.rvm/rubies/jruby-1.7.0.preview2/bin/irb:17:in `(root)'
      

        Activity

        Hide
        Charles Oliver Nutter added a comment -

        Needs a bit of porting, but the logic is found in MRI in the C "rand_range" function, called before it attempts to coerce to an Integer. We just proceed straight to the coercion.

        Show
        Charles Oliver Nutter added a comment - Needs a bit of porting, but the logic is found in MRI in the C "rand_range" function, called before it attempts to coerce to an Integer. We just proceed straight to the coercion.
        Hide
        David Kellum added a comment -

        At least the following form of this appears to actually be a regression as compared with 1.6, rather than some new feature from MRI:

        % jruby-1.6 -v -e 'puts Random.rand(0...33)'
        jruby 1.6.8 (ruby-1.9.2-p312) (2012-09-18 1772b40) (Java HotSpot(TM) Server VM 1.7.0_07) [linux-i386-java]
        21
        % jruby-1.7 -v -e 'puts Random.rand(0...33)'
        jruby 1.7.0.RC1 (1.9.3p203) 2012-09-25 8e849de on Java HotSpot(TM) Server VM 1.7.0_07-b10 [linux-i386]
        TypeError: can't convert Range into Integer
            rand at org/jruby/RubyRandom.java:261
          (root) at -e:1
        
        Show
        David Kellum added a comment - At least the following form of this appears to actually be a regression as compared with 1.6, rather than some new feature from MRI: % jruby-1.6 -v -e 'puts Random.rand(0...33)' jruby 1.6.8 (ruby-1.9.2-p312) (2012-09-18 1772b40) (Java HotSpot(TM) Server VM 1.7.0_07) [linux-i386-java] 21 % jruby-1.7 -v -e 'puts Random.rand(0...33)' jruby 1.7.0.RC1 (1.9.3p203) 2012-09-25 8e849de on Java HotSpot(TM) Server VM 1.7.0_07-b10 [linux-i386] TypeError: can't convert Range into Integer rand at org/jruby/RubyRandom.java:261 (root) at -e:1
        Hide
        Charles Oliver Nutter added a comment -
        commit 4381bc2a79d9fbc975c67abc0040215504190a71
        Author: Charles Oliver Nutter <headius@headius.com>
        Date:   Fri Sep 28 20:59:53 2012 -0500
        
            Fix JRUBY-6838: Kernel#rand(Range) not supported
            
            The logic was already in place, but appears to have been
            refactored a bit on Ruby master. We should try to match their API,
            for maintenance purposes, but this fix is good enough for now.
        
        :100644 100644 14d2ba4... 7e3e3f8... M	src/org/jruby/RubyRandom.java
        :100644 000000 2da70c3... 0000000... D	test/externals/ruby1.9/excludes/TestRand.rb
        
        Show
        Charles Oliver Nutter added a comment - commit 4381bc2a79d9fbc975c67abc0040215504190a71 Author: Charles Oliver Nutter <headius@headius.com> Date: Fri Sep 28 20:59:53 2012 -0500 Fix JRUBY-6838: Kernel#rand(Range) not supported The logic was already in place, but appears to have been refactored a bit on Ruby master. We should try to match their API, for maintenance purposes, but this fix is good enough for now. :100644 100644 14d2ba4... 7e3e3f8... M src/org/jruby/RubyRandom.java :100644 000000 2da70c3... 0000000... D test/externals/ruby1.9/excludes/TestRand.rb

          People

          • Assignee:
            Charles Oliver Nutter
            Reporter:
            Tim Olsen
          • Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: