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 Campbell <nitrodist@gmail.com>:
> 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.
>
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).
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).
You can ask the method for its owner to find out
1.method(:to_d).owner # => Integer
···
On Sat, Apr 14, 2012 at 11:45 AM, Chad Perrin <code@apotheon.net> wrote:
On Sat, Apr 14, 2012 at 07:23:34AM +0900, Mark Campbell 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 15:19 użytkownik Mark Campbell
<nitrodist@gmail.com> 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/
> >
> > 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.
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.
. . . 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?
···
On Sun, Apr 15, 2012 at 05:35:12AM +0900, Josh Cheek wrote:
On Sat, Apr 14, 2012 at 11:45 AM, Chad Perrin <code@apotheon.net> wrote:
> On Sat, Apr 14, 2012 at 07:23:34AM +0900, Mark Campbell wrote:
. . . 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?
-----Original Message-----
From: Chad Perrin [mailto:code@apotheon.net]
Sent: Sunday, April 15, 2012 10:48 AM
To: ruby-talk ML
Subject: Re: Why doesn't Fixnum#to_d exist?
On Sun, Apr 15, 2012 at 05:35:12AM +0900, Josh Cheek wrote:
On Sat, Apr 14, 2012 at 11:45 AM, Chad Perrin <code@apotheon.net> wrote:
> On Sat, Apr 14, 2012 at 07:23:34AM +0900, Mark Campbell 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.
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.
. . . 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?
> >
> > 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.
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 Sun, Apr 15, 2012 at 7:48 PM, Chad Perrin <code@apotheon.net> wrote:
On Sun, Apr 15, 2012 at 05:35:12AM +0900, Josh Cheek wrote:
On Sat, Apr 14, 2012 at 11:45 AM, Chad Perrin <code@apotheon.net> wrote:
> On Sat, Apr 14, 2012 at 07:23:34AM +0900, Mark Campbell wrote: