From: Konrad Meyer Date: 2007-11-23T13:39:02+09:00 Subject: Re: syntax Float equality within epsilon --nextPart42710690.FziPjWjx7e Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Quoth Phrogz: > On Nov 22, 1:38 am, unbewusst.s...@weltanschauung.com.invalid (Une > B=E9vue) wrote: > > i need to test float numbers within an epsilon, then i've extended the > > Float clas like that : > > > > class Float > > def =3D=3D=3D( aFloat, eps =3D 1.0e-10) > > begin > > clazz =3D aFloat.class.to_s > > raise "Argument \"#{aFloat}\" must be a Float (being of > > #{clazz})." if clazz !=3D "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 =3D 1.000000001 > > b =3D 1.00000000012 > > p ( a =3D=3D=3D( b, 0.001) ).to_s > > gave me : > > [...] syntax error, unexpected ')', expecting tCOLON2 or '[' or '.' > > p ( a =3D=3D=3D( b, 0.001) ).to_s > > ^ > > > > why ??? >=20 > 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: >=20 > irb(main):013:0> class Float > irb(main):014:1> def =3D=3D=3D( a, b ) > irb(main):015:2> p a, b > irb(main):016:2> end > irb(main):017:1> end > =3D> nil > irb(main):018:0> a =3D 5.4 > =3D> 5.4 > irb(main):019:0> a.send( :=3D=3D=3D, 42, 73 ) > 42 > 73 > =3D> nil >=20 > 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 Or you could use "normal" method call notation: irb(main):009:0> a.=3D=3D=3D(32, 73) 32 73 =3D> nil Regards, =2D-=20 Konrad Meyer http://konrad.sobertillnoon.com/ --nextPart42710690.FziPjWjx7e Content-Type: application/pgp-signature; name=signature.asc Content-Description: This is a digitally signed message part. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (GNU/Linux) iD8DBQBHRlldCHB0oCiR2cwRAtUwAKCIHcxxt5SSIPu3e2cStLlu3QmJfACdGuUw /0q84OAJIMsCzUtWTZTFa/U= =Sy5Q -----END PGP SIGNATURE----- --nextPart42710690.FziPjWjx7e--