Forum: Ruby Module specific metric methods for Numeric

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.
0b7018aed1268b4412f6b9ad0d4998d7?d=identicon&s=25 Benjamin Ter kuile (bterkuile)
on 2009-01-03 12:16
Hi,

I love ruby for it flexibility and ability to adapt, but I encountered a
problem which I don't have the answer for.
Different modules may use a different metric base. Sketchup for examples
thinks that 4 actually means 4 inch while another system might think it
is 4 meter. It would be nice to be able to get rid of these issued by
having specific Numeric methods which can change in a module. The test
script I wrote is:
0b7018aed1268b4412f6b9ad0d4998d7?d=identicon&s=25 Benjamin Ter kuile (bterkuile)
on 2009-01-03 12:22
Something went wrong and the message was submitted before I was
finished, so here the rest:

The test script I wrote is:
class Numeric
        def cm
                self*2.54
        end
end
puts "4.cm = #{4.cm}"
module Test
        class Numeric

                def cm
                        self*0.1
                end
        end
end

class Tc
        include Test
        def initialize
                puts "4.cm => #{4.cm}"
        end
end
t = Tc.new
puts "4.cm = #{4.cm}"
this results in:
4.cm = 10.16
4.cm => 10.16
4.cm = 10.16

Replacing class Numeric with class Object::Numeric will result in:
4.cm = 10.16
4.cm => 0.4
4.cm = 0.4

while the actual result that I aim for is:
4.cm = 10.16
4.cm => 0.4
4.cm = 10.16

Does anyone knows the answer to this one? Or an even better approach?

Many thanks,

Benjamin
This topic is locked and can not be replied to.