From: Robert Klemme Date: 2008-11-04T21:43:31+09:00 Subject: Re: Conditional overriding of operators 2008/11/4 Daniel Vartanov : > I am trying to replace .+ operator of Numeric class if and only if > argument (summand) is my own class. > > To be more specific: I am writing geometry module in Ruby and need to > implement vector-number multiplication so that both: > > 2 * vector > vector * 2 > > work well. > > Therefore I need to replace .+ method of Numeric without spoiling > arithmetical operations with other classes. No, you don't. You need to implement #coerce and #+, #- etc. in *your* class properly (see the link Pit provided for one, albeit not very complete example). Basically your coerce is invoked when Fixnum#+ sees an instance of your class so you are in control what operation is finally implemented. You can find more hits with http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb?key=coerce&cginame=namazu.rb&submit=Search&dbname=ruby-talk&max=50&whence=0 Cheers robert -- remember.guy do |as, often| as.you_can - without end