From e3b45fddfc49c8def9fe4a6fc0af4bf722632a84 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Fri, 7 Aug 2015 17:08:58 +0200 Subject: [PATCH] to_float support for non String values --- lib/oga/xpath/conversion.rb | 6 +----- spec/oga/xpath/conversion_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/oga/xpath/conversion.rb b/lib/oga/xpath/conversion.rb index ed95f9f..1146427 100644 --- a/lib/oga/xpath/conversion.rb +++ b/lib/oga/xpath/conversion.rb @@ -63,11 +63,7 @@ module Oga value = value.text end - if value.is_a?(String) - value = Float(value) rescue Float::NAN - end - - value + Float(value) rescue Float::NAN end # @return [TrueClass|FalseClass] diff --git a/spec/oga/xpath/conversion_spec.rb b/spec/oga/xpath/conversion_spec.rb index b7ea936..6737b62 100644 --- a/spec/oga/xpath/conversion_spec.rb +++ b/spec/oga/xpath/conversion_spec.rb @@ -176,6 +176,12 @@ describe Oga::XPath::Conversion do described_class.to_float(node).should == 10.5 end end + + describe 'using a NilClass' do + it 'returns Float::NAN' do + described_class.to_float(nil).should be_nan + end + end end describe 'to_boolean' do