Thursday, May 7, 2009

Fun With JRuby IRB Console

An example of playing around with the JRuby IRB Console in JRuby 1.2.0 with Java 1.6.0_07:
$ jruby -v
jruby 1.2.0 (ruby 1.8.6 patchlevel 287) (2009-03-16 rev 9419) [i386-java]

$ java -version
java version "1.6.0_07"
Java(TM) SE Runtime Environment (build 1.6.0_07-b06-153)
Java HotSpot(TM) 64-Bit Server VM (build 1.6.0_07-b06-57, mixed mode)

$ jruby -S irb
irb(main):001:0> require 'java'
=> false
irb(main):002:0> s = Java::JavaLang::String.new("A Java String")
=> #<Java::JavaLang::String:0x2f15b0 @java_object=#<Java::JavaObject:0x84c229>>
irb(main):003:0> s = s.replaceAll("Java", "Much Cooler")
=> "A Much Cooler String"
irb(main):004:0> s.length
=> 20
irb(main):005:0> list = Java::JavaUtil::ArrayList.new
=> #<Java::JavaUtil::ArrayList:0xfb480a @java_object=#<Java::JavaObject:0x369763>>
irb(main):006:0> list.add("a string")
=> true
irb(main):007:0> list.add(Java::JavaLang::Integer.new(5))
=> true
irb(main):008:0> list
=> #<Java::JavaUtil::ArrayList:0xfb480a @java_object=#<Java::JavaObject:0x369763>>
irb(main):009:0> list.get(0)
=> "a string"
irb(main):010:0> list.get(1)
=> 5
irb(main):011:0> list.get(1).class
=> Fixnum
irb(main):012:0> Java::JavaLang::Integer.new(5).java_class
=> #<Java::JavaClass:0x447c32>
irb(main):013:0> Java::JavaLang::Integer.new(5).java_class.name
=> "java.lang.Integer"
irb(main):014:0> Java::JavaLang::Integer.new(5)                
=> #<Java::JavaLang::Integer:0x334bfc @java_object=#<Java::JavaObject:0xc1b26c>>
irb(main):015:0> Java::JavaLang::Integer.parseInt("5")
=> 5
irb(main):016:0> Java::JavaLang::Integer.parseInt("5").class
=> Fixnum
irb(main):017:0> Java::JavaLang::Integer.parseInt("999999999999999999999999999999999999").class
NativeException: java.lang.NumberFormatException: For input string: "999999999999999999999999999999999999"
 from java/lang/NumberFormatException.java:48:in `forInputString'
 from java/lang/Integer.java:459:in `parseInt'
 from java/lang/Integer.java:497:in `parseInt'
 from sun/reflect/NativeMethodAccessorImpl.java:-2:in `invoke0'
 from sun/reflect/NativeMethodAccessorImpl.java:39:in `invoke'
 from sun/reflect/DelegatingMethodAccessorImpl.java:25:in `invoke'
 from java/lang/reflect/Method.java:585:in `invoke'
 from org/jruby/javasupport/JavaMethod.java:298:in `invokeWithExceptionHandling'
 from org/jruby/javasupport/JavaMethod.java:278:in `invoke_static'
 from org/jruby/java/invokers/StaticMethodInvoker.java:47:in `call'
 from org/jruby/runtime/callsite/CachingCallSite.java:273:in `cacheAndCall'
 from org/jruby/runtime/callsite/CachingCallSite.java:112:in `call'
 from org/jruby/ast/CallOneArgNode.java:57:in `interpret'
 from org/jruby/ast/CallNoArgNode.java:61:in `interpret'
 from org/jruby/ast/NewlineNode.java:104:in `interpret'
 from org/jruby/ast/RootNode.java:129:in `interpret'
... 97 levels...
 from org/jruby/runtime/callsite/CachingCallSite.java:136:in `callIter'
 from org/jruby/ast/FCallOneArgBlockNode.java:34:in `interpret'
 from org/jruby/ast/NewlineNode.java:104:in `interpret'
 from org/jruby/ast/BlockNode.java:71:in `interpret'
 from org/jruby/internal/runtime/methods/InterpretedMethod.java:163:in `call'
 from org/jruby/internal/runtime/methods/DefaultMethod.java:144:in `call'
 from org/jruby/runtime/callsite/CachingCallSite.java:273:in `cacheAndCall'
 from org/jruby/runtime/callsite/CachingCallSite.java:112:in `call'
 from Users/garyweaver/jruby/bin/jirb:19:in `__file__'
 from Users/garyweaver/jruby/bin/jirb:-1:in `load'
 from org/jruby/Ruby.java:577:in `runScript'
 from org/jruby/Ruby.java:480:in `runNormally'
 from org/jruby/Ruby.java:354:in `runFromMain'
 from org/jruby/Main.java:229:in `run'
 from org/jruby/Main.java:110:in `run'
 from org/jruby/Main.java:94:in `main'irb(main):018:0> 
irb(main):019:0* 999999999999999999999999999999999999      
=> 999999999999999999999999999999999999
irb(main):020:0> 999999999999999999999999999999999999.class
=> Bignum
irb(main):021:0> Java::JavaLang::Integer.new(999999999999999999999999999999999999)
RangeError: bignum too big to convert into `long'
 from (irb):22
irb(main):022:0> Java::JavaLang::Integer.new(-999999999999999999999999999999999999)
RangeError: bignum too big to convert into `long'
 from (irb):23
irb(main):023:0> 999999999999999999999999999999999999.999999999999999999999999999999999999
=> 1.0e+36
irb(main):024:0> (999999999999999999999999999999999999.999999999999999999999999999999999999).class
=> Float
irb(main):025:0> Java::JavaLang::Double.new(999999999999999999999999999999999999.999999999999999999999999999999999999)
=> #<Java::JavaLang::Double:0x960f0e @java_object=#<Java::JavaObject:0xb66175>>
irb(main):026:0> Java::JavaLang::Double.new(999999999999999999999999999999999999.999999999999999999999999999999999999).java_class.name
=> "java.lang.Double"
irb(main):027:0> Java::JavaLang::Double.parseDouble("999999999999999999999999999999999999.999999999999999999999999999999999999")                
=> 1.0e+36
irb(main):028:0> Java::JavaLang::Double.parseDouble("999999999999999999999999999999999999.999999999999999999999999999999999999").class
=> Float
irb(main):029:0> map = Java::JavaUtil::HashMap.new
=> #<Java::JavaUtil::HashMap:0x619488 @java_object=#<Java::JavaObject:0x5e8588>>
irb(main):030:0> map.put("1","1")
=> nil
irb(main):031:0> map
=> #<Java::JavaUtil::HashMap:0x619488 @java_object=#<Java::JavaObject:0x5e8588>>
irb(main):032:0> map.get("1")
=> "1"
irb(main):033:0> map.put("2",nil)
=> nil
irb(main):034:0> map.get("2")
=> nil
irb(main):035:0> map.put("3",999999999999999999999999999999999999.999999999999999999999999999999999999)
=> nil
irb(main):036:0> map.get("3")
=> 9.99999961690316e+35
irb(main):037:0> map.get("3").class
=> Float

No comments: