From: yermej Date: 2007-11-23T15:10:03+09:00 Subject: Re: syntax Float equality within epsilon On Nov 22, 10:16 pm, Phrogz wrote: > On Nov 22, 1:38 am, unbewusst.s...@weltanschauung.com.invalid (Une > > > > Bévue) wrote: > > i need to test float numbers within an epsilon, then i've extended the > > Float clas like that : > > > class Float > > def ===( aFloat, eps = 1.0e-10) > > begin > > clazz = aFloat.class.to_s > > raise "Argument \"#{aFloat}\" must be a Float (being of > > #{clazz})." if clazz != "Float" > > ( self > aFloat - eps ) && ( self < aFloat + eps ) > > rescue > > puts "An error occurred: #{$!}" > > nil > > end > > end > > end > > > this works as expected except when i want not to use the default value > > for eps where i couldn't find the correct syntax : > > a = 1.000000001 > > b = 1.00000000012 > > p ( a ===( b, 0.001) ).to_s > > gave me : > > [...] syntax error, unexpected ')', expecting tCOLON2 or '[' or '.' > > p ( a ===( b, 0.001) ).to_s > > ^ > > > why ??? > > Some operator methods have syntax sugar that prevents them from taking > multiple arguments using simple syntax. You can get around this, > albeit with a slightly less elegant syntax: > > irb(main):013:0> class Float > irb(main):014:1> def ===( a, b ) > irb(main):015:2> p a, b > irb(main):016:2> end > irb(main):017:1> end > => nil > irb(main):018:0> a = 5.4 > => 5.4 > irb(main):019:0> a.send( :===, 42, 73 ) > 42 > 73 > => nil > > If this is a case you'd use often, I suggest simply giving it a normal > method name. that you can call without using #send It can still be called without send. In place of a.send... in the above example use: a.===(42, 73) Still not pretty, but a bit shorter. Jeremy