Search code examples
rubybigdecimal

Any need to use BigDecimal.new method?


I've seen following code in the docs for the BigDecimal class:

Similarly:

(::new(“1.2”) - BigDecimal(“1.0”)) == BigDecimal(“0.2”) -> true
(1.2 - 1.0) == 0.2 -> false

So I was wondering if it is neccessary to use the new method like this: BigDecimal.new 2.5, 2. Or is it just bad practice using just BigDecimal 2.5, 2? When evaluating the same BigDecimal value, they all get new references:

BigDecimal 2.5, 2
#=> #<BigDecimal:7ffa93e524b0,'0.25E1',18(36)> 
BigDecimal 2.5, 2
#=> #<BigDecimal:7ffa97236600,'0.25E1',18(36)>

Solution

  • Well, in the latest kernel doc we can see that BigDecimal method looks like this:

    static VALUE
    BigDecimal_global_new(int argc, VALUE *argv, VALUE self)
    {
        return BigDecimal_new(argc, argv, rb_cBigDecimal);
    }
    

    In other words, BigDecimal.new will be called over the supplied arguments. So, yes, it's not strictly necessary to use it explicitly.