From: Jim Freeze Date: 2003-04-28T08:51:19+09:00 Subject: Re: MathN On Monday, 28 April 2003 at 7:16:39 +0900, Bret Jolly wrote: > > # make infix operators in Numeric classes play nice with Pnom > PlayNiceWithPnom = {'+'=>'Plus', '*'=>'Mul', '<=>'=>'Spaceship', > '=='=>'Equal', '<'=>'Less', '>'=>'Greater', '<='=>'LessEqual', > '>='=>'GreaterEqual'} > PlayNiceWithPnom.each {|key, value| > eval(%Q% PlayNiceWithPnom#{value} = <<-EOS > alias prePnom_#{value} #{key} > def #{key}(other) > if defined?(Pnom) and other.kind_of?(Pnom) > Pnom.new(self) #{key} other > else > prePnom_#{value}(other) > end > end > EOS > %) > } > ObjectSpace.each_object(Class) {|klass| > if klass.ancestors.include?(Numeric) > PlayNiceWithPnom.each{|key, value| > if klass.public_instance_methods.include?(key) > unless klass.public_instance_methods.include?("prePnom_#{value}") > eval(%Q% klass.module_eval(PlayNiceWithPnom#{value})%) > end > end > } > end > } > > I shudder to think of trying to accomplish this in Python! The above seems very interesting, but it is not clear to me exactly how it works. Would you mind providing a walk-thru description of what it does? Thanks -- Jim Freeze ---------- Don't change the reason, just change the excuses! -- Joe Cointment