danielpclark / rutie

β€œThe Tie Between Ruby and Rust.”
MIT License
940 stars 62 forks source link

Correctly handle Float conversion of Ruby numerics #100

Closed bbugh closed 5 years ago

bbugh commented 5 years ago

This PR fixes #99 that I reported yesterday.

It adds support for Ruby's other numeric types Fixnum, Bignum, and Rational, which can be automatically cast by the Ruby CPI to a Float. I did not add Complex support, because it's not 100% safe (e.g. Complex(1, 1) cannot be cast to a Float).

Result:

fn rutie_float_test(base: Float) -> Float {
  Float::new(base.unwrap().to_f64() * 2.0)
}
# Before this patch
RutieExample.rutie_float_test(10)
# PANIC!! πŸ’€

# Before this patch
RutieExample.rutie_float_test(1/2r)
# PANIC!! πŸ’€

# After this patch
RutieExample.rutie_float_test(10) 
# => 20 😎
bbugh commented 5 years ago

Uncertain about these tests failing in integer, which I didn't touch... but this is the wrong approach anyway.