Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions lib/inifile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,17 @@ def typecast( value )
when %r/\Afalse\z/i; false
when %r/\A\s*\z/i; nil
else
Integer(value) rescue \
Float(value) rescue \
unescape_value(value)
stripped_value = value.strip
if stripped_value =~ /^\d*\.\d+$/
Float(stripped_value)
elsif stripped_value =~ /^[^0]\d*$/
Integer(stripped_value)
else
unescape_value(value)
end
end
rescue
unescape_value(value)
end

# Unescape special characters found in the value string. This will convert
Expand Down
7 changes: 7 additions & 0 deletions test/data/typecast.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[section_one]
int1 = 146
float1 = 0.35
float2 = .634
float3 = 234.646
string1 = 00342
string2 = 345.
16 changes: 16 additions & 0 deletions test/test_inifile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -557,5 +557,21 @@ def test_empty_comment_string
assert_equal 3, ini_file['section_one']['one']
assert_equal 5, ini_file['section_five']['five']
end

def test_integer_typecast
ini_file = IniFile.load('test/data/typecast.ini')
assert_equal 146, ini_file['section_one']['int1']
assert_equal "00342", ini_file['section_one']['string1']
end

def test_float_typecast
ini_file = IniFile.load('test/data/typecast.ini')
assert_equal 0.35, ini_file['section_one']['float1']
assert_equal 0.634, ini_file['section_one']['float2']
assert_equal 234.646, ini_file['section_one']['float3']
assert_equal "345.", ini_file['section_one']['string2']
end


end