Forum: Ruby Where is Fixnum#power! coming from?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Daniel B. (Guest)
on 2006-03-29 23:25
(Received via mailing list)
Hi all,

Ruby 1.8.4

I realize there's a Fixnum#power! method defined in rational.rb, but it
appears
that Fixnum has that method available to it whether or not I require
rational.rb.  I looked through the Ruby source and didn't see anything.

djberge@~-710>irb
irb(main):001:0> Fixnum.instance_methods(false).grep(/power/)
=> ["rpower", "power!"]

So, where the heck is it coming from?

Thanks,

Dan
Daniel H. (Guest)
on 2006-03-29 23:37
(Received via mailing list)
On Mar 29, 2006, at 9:22 PM, Daniel B. wrote:

> I realize there's a Fixnum#power! method defined in rational.rb,
> but it appears that Fixnum has that method available to it whether
> or not I require rational.rb.  I looked through the Ruby source and
> didn't see anything.
>
> djberge@~-710>irb
> irb(main):001:0> Fixnum.instance_methods(false).grep(/power/)
> => ["rpower", "power!"]
>
> So, where the heck is it coming from?

$ irb -rubygems
 > 2.power!(3)
=> 8
 > exit
$ irb
 > 2.power!(3)
NoMethodError: undefined method `power!' for 2:Fixnum
         from (irb):1
         from :0

Are you requiring rubygems?

-- Daniel
Daniel B. (Guest)
on 2006-03-29 23:40
(Received via mailing list)
Daniel H. wrote:
>>
>         from :0
>
> Are you requiring rubygems?
>
> -- Daniel

Aha!  I'm not explicitly require'ing it, but my $RUBYOPT was set.  Once
I unset
it, I got the expected error.

Thanks,

Dan
Marcin MielżyÅ?ski (Guest)
on 2006-03-29 23:47
(Received via mailing list)
Daniel B. wrote:
> Hi all,
>
> Ruby 1.8.4
>
> I realize there's a Fixnum#power! method defined in rational.rb, but it
> appears that Fixnum has that method available to it whether or not I
> require rational.rb.  I looked through the Ruby source and didn't see
> anything.
>


numeric.c:
flo_pow(x, y)...
fix_pow(x, y)...

and then:
rb_define_method(rb_cFixnum, "**", fix_pow, 1);
rb_define_method(rb_cFloat, "**", flo_pow, 1);

and then in rational.rb:

class Fixnum
    alias power! **
end

are You sure irb doesnt use rational.rb ?

lopex
Jim W. (Guest)
on 2006-03-30 01:51
Daniel B. wrote:
> Daniel H. wrote:
>>>
>>         from :0
>>
>> Are you requiring rubygems?
>>
>> -- Daniel
>
> Aha!  I'm not explicitly require'ing it, but my $RUBYOPT was set.  Once
> I unset
> it, I got the expected error.

FYI:

RubyGems uses time.rb
time.rb uses parsedate.rb
parsedate.rb uses date/format.rb
date/format.rb uses rational.rb

--
-- Jim W.
This topic is locked and can not be replied to.