On Thu, Dec 4, 2014 at 10:13 PM, Colin Bartlett <colinb2r / googlemail.com> wrote:
> To avoid ambiguity it might be helpful if the documentation for
> BigDecimal#fix said that it returned the integer part of the value as
> a BigDecimal.

Generally math operations return the most appropriate type.  For
example from a Fixnum multiplication you can get back a Fixnum or a
Bignum.  You really should not be caring about the type too much, I
think.  Note that even printf will happily output a BigDecimal as
integer value.

> This behaviour could be useful if you want to convert something to an
> "integer value", and then want to be able to divide into that "integer
> value" without the result being truncated to an integer.

It is also useful to avoid unnecessary conversions.  By returning
BigDecimal method #fix probably does less work. If a conversion to
Fixnum or Bignum was integrated then the user could never get a
BigDecimal out of #fix and it may have to be recreated during a
#coerce call done in another math operation.  So that would mean a lot
unnecessary work.

> If you really need an Integer then BigDecimal#to_i and
> BigDecimal#floor seem to work.

I'd rather use #to_int than #to_i because that is the proper method
which enforces integerness.

If you are interested in how numeric classes usually work in Ruby I
have blogged about this a looong time ago:
http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html

Kind regards

robert


-- 
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/