Skip to content
This repository has been archived by the owner on Dec 18, 2019. It is now read-only.

Commit

Permalink
Address gotcha wherein JRuby coerces numeric inputs to Java Float/Lon…
Browse files Browse the repository at this point in the history
…g constructors
  • Loading branch information
mrwalker committed Aug 12, 2011
1 parent e3ee3ac commit 0ba1857
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/cascading/expr_stub.rb
Expand Up @@ -109,8 +109,12 @@ def specific_to_java(value, type)
# would fail. We therefore punt if value is a String.
return value if value.kind_of?(::String)
case type
when java.lang.Float.java_class then java.lang.Float.new(value) rescue value
when java.lang.Long.java_class then java.lang.Long.new(value) rescue value
when java.lang.Float.java_class
return value if value.kind_of?(::Integer)
java.lang.Float.new(value) rescue value
when java.lang.Long.java_class
return value if value.kind_of?(::Float)
java.lang.Long.new(value) rescue value
else value
end
end
Expand Down
8 changes: 8 additions & 0 deletions spec/expr_spec.rb
Expand Up @@ -76,6 +76,14 @@
e = ExprStub.new('x:long + y:int')
lambda{ e.eval(:x => '2', :y => 3) }.should raise_error ExprArgException

# eval should not coerce floats to Java Longs
e = ExprStub.new('x:long + y:int')
lambda{ e.eval(:x => 2.0, :y => 3) }.should raise_error ExprArgException

# eval should not coerce integers to Java Floats
e = ExprStub.new('x:int + y:float')
lambda{ e.eval(:x => 2, :y => 3) }.should raise_error ExprArgException

e = ExprStub.new('x:float + y:int')
lambda{ e.eval(:x => 'blah', :y => 3) }.should raise_error ExprArgException

Expand Down

0 comments on commit 0ba1857

Please sign in to comment.