Hello,

I am totally puzzled by this application of the .to_f method. I have
been using it without problem, but now it fails me in a strange way.
I have a text file "ssim-test-vts_01-0.txt" with just two lines where I
want to get the last number:
     SSIM: Structural Similarity Index Metric 0.23
     Average SSIM= 78.90848567
When I test my code in a short program:
<start code>
def is_a_number?(s)
     s.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
end

derligne = File.open("ssim-test-vts_01-0.txt") { |f| f.inject { |_,ln|
ln }
          }
derligne = derligne.chomp
puts "derligne: #{derligne}"
ssm = derligne.scan(/[-+]?\d*\.?\d+/)
puts "ssm[0]: #{ssm[0]}"
if is_a_number?(ssm[0])
  puts "Is a number"
else
  puts "Is not a number"
end
ssim = ssm[0].to_f
erreur = (ssim - 83).abs
puts "ssim = #{ssim}"
puts "erreur = #{erreur}"
puts "-------------------------------"
<end code>

I get the correct output:
   F:\_videos to do>der
   derligne: Average SSIM= 78.90848567
   ssm[0]: 78.90848567
   Is a number
   ssim = 78.90848567
   erreur = 4.091514329999995

But when the exact same code is part of a larger program, it tells me:
   derligne: Average SSIM= 78.90848567
   ssm[0]: 78.90848567
   Is a number
   TypeError
   String can't be coerced into Float
I can't figure it out. .to_f works normally in other places.
I also tried Float(ssm[0]) with the same result.
Any help would be appreciated.

-- 
Posted via http://www.ruby-forum.com/.