* operator, Float

  • Thread starter Иван Сташко
  • Start date
Ð

Иван Сташко

Where is the * operator defined for Float?

ree-1.8.7-2010.02 > 3.0.methods.grep "*"
=> ["*"]
ree-1.8.7-2010.02 > 3.0.class
=> Float
ree-1.8.7-2010.02 > Float.methods.grep "*"
=> []
ree-1.8.7-2010.02 > Float.superclass
=> Numeric
ree-1.8.7-2010.02 > Numeric.methods.grep "*"
=> []
ree-1.8.7-2010.02 > Numeric.superclass
=> Object
ree-1.8.7-2010.02 > Object.methods.grep "*"
=> []
ree-1.8.7-2010.02 > Object.superclass
=> nil

I'm guessing there's a mixin somewhere or this has to do with
eigenclasses or perhaps a C-module has been mixed-in between Numeric and
Float. Where do I look at this code?
 
M

Marvin Gülker

Иван Сташко said:
Where is the * operator defined for Float?

ree-1.8.7-2010.02 > 3.0.methods.grep "*"
=> ["*"]
ree-1.8.7-2010.02 > 3.0.class
=> Float
ree-1.8.7-2010.02 > Float.methods.grep "*"
=> []
ree-1.8.7-2010.02 > Float.superclass
=> Numeric
ree-1.8.7-2010.02 > Numeric.methods.grep "*"
=> []
ree-1.8.7-2010.02 > Numeric.superclass
=> Object
ree-1.8.7-2010.02 > Object.methods.grep "*"
=> []
ree-1.8.7-2010.02 > Object.superclass
=> nil

I'm guessing there's a mixin somewhere or this has to do with
eigenclasses or perhaps a C-module has been mixed-in between Numeric and
Float. Where do I look at this code?

The way you're trying to find the #* method is wrong. You try to find
Class#*, not Float#*. Try it this way:

irb(main):004:0> Float.public_instance_methods.map(&:to_s).grep("*")
=> ["*"]
irb(main):005:0> Numeric.public_instance_methods.map(&:to_s).grep("*")
=> []
irb(main):006:0>

If Float#* wasn't defined in C code, you could use
Method#source_location to find out where it is defined (at least in Ruby
1.9):

irb(main):006:0> 3.0.method:)*).source_location
=> nil
irb(main):007:0> require "pathname"
=> true
irb(main):008:0> Pathname.new("x").method:)to_s).source_location
=> ["/opt/rubies/ruby-1.9.1-p429/lib/ruby/1.9.1/pathname.rb", 248]
irb(main):009:0>

If #source_location gives you nil, you're dealing with a C method.

Marvin
 
K

Ken Bloom

Where is the * operator defined for Float?

use instance_methods, not methods

Float.instance_methods(false).grep("*")
ree-1.8.7-2010.02 > 3.0.methods.grep "*"
=> ["*"]
ree-1.8.7-2010.02 > 3.0.class
=> Float
ree-1.8.7-2010.02 > Float.methods.grep "*"
=> []
ree-1.8.7-2010.02 > Float.superclass
=> Numeric
ree-1.8.7-2010.02 > Numeric.methods.grep "*"
=> []
ree-1.8.7-2010.02 > Numeric.superclass
=> Object
ree-1.8.7-2010.02 > Object.methods.grep "*"
=> []
ree-1.8.7-2010.02 > Object.superclass
=> nil

I'm guessing there's a mixin somewhere or this has to do with
eigenclasses or perhaps a C-module has been mixed-in between Numeric and
Float. Where do I look at this code?
 
Ð

Иван Сташко

OK, this is clear.

I was able to get the necessary effect using coerce(). What failed was
...

class Float
def *(o)
if o.class == FooBoo then
o.send:)*,o)
else
super
end
end
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,833
Members
47,378
Latest member
BlakeLig

Latest Threads

Top