Why doesn't Fixnum#to_d exist?

http://stdlib.rubyonrails.org/libdoc/bigdecimal/rdoc/index.html

String#
to_d … to BigDecimal

Float#
to_d … to BigDecimal

Rational#
to_d … to BigDecimal

These all exist… why not for Fixnum#to_d? Right now I’m having to
write a
mixin on Fixnum, something like this:

class Fixnum
def to_d
BigDecimal(self.to_s)
end
end

The above code is very similar to what has been written for Float and
String.

Is there a reason why this doesn’t exist on Fixnum? I’m currently using
1.8.7.

It does exist, but it’s in a separate file - bigdecimal/util or utils,
if I remember correctly. (I’m on my mobile, can’t check.)

2012/4/13, Mark C. [email protected]:

Yes, that file has the String#to_d, Float#to_d and Rational#to_d.
Fixnum#to_d is not present.

2012/4/13 Bartosz Dziewoński [email protected]

I totally agree with you, however, as I mentioned in my first post, I am
using 1.8.7, which does not have this. In fact, 1.9.2 doesn’t have it
either:

1.8.7 :001 > require ‘bigdecimal’
=> true
1.8.7 :002 > require ‘bigdecimal/util’
=> true
1.8.7 :003 > 25.to_d
NoMethodError: undefined method to_d' for 25:Fixnum from (irb):3 1.8.7 :004 > mark@mark-desktop ~$ rvm use 1.9.2 Using /home/mark/.rvm/gems/ruby-1.9.2-p290 mark@mark-desktop ~$ irb 1.9.2p290 :001 > require 'bigdecimal' => true 1.9.2p290 :002 > require 'bigdecimal/util' => true 1.9.2p290 :003 > 25.to_d NoMethodError: undefined method to_d’ for 25:Fixnum
from (irb):3
from /home/mark/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `’
1.9.2p290 :004 >
mark@mark-desktop ~$ rvm use 1.9.3
Using /home/mark/.rvm/gems/ruby-1.9.3-p125
mark@mark-desktop ~$ irb
1.9.3p125 :001 > require ‘bigdecimal’
=> true
1.9.3p125 :002 > require ‘bigdecimal/util’
=> true
1.9.3p125 :003 > 25.to_d
=> #BigDecimal:17ff778,‘0.25E2’,9(36)
1.9.3p125 :004 >

So, can we add this to stdlib of 1.8.7 and 1.9.2?

2012/4/14 Bartosz Dziewoński [email protected]

irb
irb(main):001:0> require ‘bigdecimal’
=> true
irb(main):002:0> require ‘bigdecimal/util’
=> true
irb(main):003:0> 25.to_d
=> #BigDecimal:1068060,‘0.25E2’,9(36)

I’m running Ruby 1.9.3 on Windows XP. According to the docs[1], #to_d
is defined for BigDecimal, Float, Rational, String, and Integer (which
is a superclass of Fixnum and Bignum).

[1]
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/bigdecimal/rdoc/index.html

– Matma R.

On Sat, Apr 14, 2012 at 07:23:34AM +0900, Mark C. wrote:

Yes, that file has the String#to_d, Float#to_d and Rational#to_d.
Fixnum#to_d is not present.

Strange. It appears you’re right. I can do 1.to_d, but I don’t know
what class has the method when I use it like that.

W dniu 14 kwietnia 2012 18:45 użytkownik Chad P.
[email protected] napisał:

On Sat, Apr 14, 2012 at 07:23:34AM +0900, Mark C. wrote:

Yes, that file has the String#to_d, Float#to_d and Rational#to_d.
Fixnum#to_d is not present.

Strange. It appears you’re right. I can do 1.to_d, but I don’t know
what class has the method when I use it like that.

It’s Integer (assuming you’re on 1.9.3).

– Matma R.

On Sat, Apr 14, 2012 at 11:45 AM, Chad P. [email protected] wrote:

You can ask the method for its owner to find out
1.method(:to_d).owner # => Integer

W dniu 14 kwietnia 2012 15:19 użytkownik Mark C.
[email protected] napisał:

I totally agree with you, however, as I mentioned in my first post, I am
using 1.8.7, which does not have this. In fact, 1.9.2 doesn’t have it
either.

Ruby 1.8.7 is nearing it’s EOL[1], so I suggest upgrading soon (if you
can). You can report issues and feature requests on Ruby’s bug
tracker: http://bugs.ruby-lang.org/

[1] Feature #4996: About 1.8.7 EOL - Backport187 - Ruby Issue Tracking System

– Matma R.

W dniu 15 kwietnia 2012 19:48 użytkownik Chad P.
[email protected] napisał:

. . . so, because I was using the Integer.methods.include? approach
earlier, I didn’t find it in Integer either. Shouldn’t the class report
the method when asked even if it’s added by a mixin or straight-up
monkeypatch?

irb(main):007:0> require ‘bigdecimal/util’
=> true
irb(main):011:0> Integer.instance_methods.grep /to_d/
=> [:to_d]

– Matma R.

On Sun, Apr 15, 2012 at 05:35:12AM +0900, Josh C. wrote:

1.method(:to_d).owner # => Integer
Thanks. That’s the method I was trying to remember, but it wouldn’t
come
to me.

It’s pretty strange how this works, though:

>> Integer.methods.include? :to_d
=> false
>> 1.method(:to_d).owner
=> Integer

. . . so, because I was using the Integer.methods.include? approach
earlier, I didn’t find it in Integer either. Shouldn’t the class report
the method when asked even if it’s added by a mixin or straight-up
monkeypatch?

You should have used Integer.instance_methods.

Integer.methods gives ‘class methods’.

-Jingjing

On Mon, Apr 16, 2012 at 05:59:32AM +0900, Duan, Jingjing wrote:

You should have used Integer.instance_methods.

Integer.methods gives ‘class methods’.

/facepalm

Yes, of course you’re right. I retreat to my corner once more.

On Sun, Apr 15, 2012 at 7:48 PM, Chad P. [email protected] wrote:

You can ask the method for its owner to find out
1.method(:to_d).owner # => Integer

Thanks. That’s the method I was trying to remember, but it wouldn’t come
to me.

It’s pretty strange how this works, though:

Integer.methods.include? :to_d
=> false

Integer.methods will return methods of the instance Integer (i.e. the
class object)! You rather want Integer.instance_methods.

1.method(:to_d).owner
=> Integer

. . . so, because I was using the Integer.methods.include? approach
earlier, I didn’t find it in Integer either. Shouldn’t the class report
the method when asked even if it’s added by a mixin or straight-up
monkeypatch?

See above.

Kind regards

robert

On Tue, Apr 17, 2012 at 3:36 PM, Robert K.
[email protected] wrote:

Integer.methods will return methods of the instance Integer (i.e. the
class object)! You rather want Integer.instance_methods.

Sorry for the noise, I had overlooked Jingjing’s message.

Kind regards

robert