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 ???
On Nov 22, 1:38 am, [email protected] (Une
Bévue) wrote:
rescue
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
Quoth Phrogz:
#{clazz})." if clazz != “Float”
a = 1.000000001
multiple arguments using simple syntax. You can get around this,
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
Or you could use “normal” method call notation:
irb(main):009:0> a.===(32, 73)
32
73
=> nil
Regards,
On Nov 22, 10:16 pm, Phrogz [email protected] wrote:
begin
why ???
=> 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
yermej [email protected] wrote:
a.===(42, 73)
Still not pretty, but a bit shorter.
fine, thanks this works great to me !