Skip to content

Error messages for failed casts using Integer() subtly different to MRI #3346

@dwaller

Description

@dwaller

A trivial problem that I fell over because I had tests that asserted on the error message. But maybe it has an equally trivial solution.

MRI gives the error message invalid value for Integer(): "3a"

JRuby gives the error message invalid value for Integer: "3a" Note, no () after Integer

➜  code  ruby --version
ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
➜  code  irb
 :001 > Integer('3a')
ArgumentError: invalid value for Integer(): "3a"
    from (irb):1:in `Integer'
    from (irb):1
    from /Users/dwaller/.rvm/rubies/ruby-2.2.3/bin/irb:15:in `<main>'
 :002 > Integer('')
ArgumentError: invalid value for Integer(): ""
    from (irb):2:in `Integer'
    from (irb):2
    from /Users/dwaller/.rvm/rubies/ruby-2.2.3/bin/irb:15:in `<main>'
 :003 > Integer(nil)
TypeError: can't convert nil into Integer
    from (irb):3:in `Integer'
    from (irb):3
    from /Users/dwaller/.rvm/rubies/ruby-2.2.3/bin/irb:15:in `<main>'
➜  code  rvm use jruby-9.0.1.0-d19
Using /Users/dwaller/.rvm/gems/jruby-9.0.1.0-d19
➜  code  ruby --version
jruby 9.0.1.0 (2.2.2) 2015-09-02 583f336 Java HotSpot(TM) 64-Bit Server VM 25.45-b02 on 1.8.0_45-b14 +jit [darwin-x86_64]
➜  code  irb
jruby-9.0.1.0-d19 :001 > Integer('3a')
ArgumentError: invalid value for Integer: "3a"
    from org/jruby/RubyKernel.java:427:in `Integer'
    from (irb):1:in `<eval>'
    from org/jruby/RubyKernel.java:978:in `eval'
    from org/jruby/RubyKernel.java:1291:in `loop'
    from org/jruby/RubyKernel.java:1098:in `catch'
    from org/jruby/RubyKernel.java:1098:in `catch'
    from /Users/dwaller/.rvm/rubies/jruby-9.0.1.0-d19/bin/irb:13:in `<top>'
jruby-9.0.1.0-d19 :002 > Integer('')
ArgumentError: invalid value for Integer: ""
    from org/jruby/RubyKernel.java:427:in `Integer'
    from (irb):2:in `<eval>'
    from org/jruby/RubyKernel.java:978:in `eval'
    from org/jruby/RubyKernel.java:1291:in `loop'
    from org/jruby/RubyKernel.java:1098:in `catch'
    from org/jruby/RubyKernel.java:1098:in `catch'
    from /Users/dwaller/.rvm/rubies/jruby-9.0.1.0-d19/bin/irb:13:in `<top>'
jruby-9.0.1.0-d19 :003 > Integer(nil)
TypeError: can't convert nil into Integer
    from org/jruby/RubyKernel.java:427:in `Integer'
    from (irb):3:in `<eval>'
    from org/jruby/RubyKernel.java:978:in `eval'
    from org/jruby/RubyKernel.java:1291:in `loop'
    from org/jruby/RubyKernel.java:1098:in `catch'
    from org/jruby/RubyKernel.java:1098:in `catch'
    from /Users/dwaller/.rvm/rubies/jruby-9.0.1.0-d19/bin/irb:13:in `<top>'

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions