INPUT = 'value=13.5'
# desired output
# <float>13.5</float>
val = INPUT.split('=')[1]
puts "<float>#{val}</float>"
# total INPUTS
=begin
value=13.5
value= '=> nope!'
value = 16
value= .3
=end
# desired output:
# Determine the type...
# float, int, string
data = <<-DATA.chomp
value=13.5
value= '=> nope!'
value = 16
value= .3
DATA
# Ior can't get rid of new-line.. because some times val is nil
data.each_line {|ln|
val = ln.split('=')[1]
puts "<float>#{val}</float>"
}
p 'ADDING COMPLEXITY %s' % ['-' * 40]
data.each_line {|ln|
val = ln.split('=')[1]
val.strip! if val
puts "<float>#{val}</float>"
}
# Now Ior needs to find out if the result is a number..
p 'ADDING COMPLEXITY 2 %s' % ['-' * 40]
data.each_line {|ln|
type = 'string'
val = ln.split('=')[1]
val.strip! if val
if val =~ /^\d*$/
type = 'integer'
elsif val =~ /^\d*\.\d*$/
type = 'float'
else
type = 'string' # Ior forgot it's already 'string'
end
puts "<#{type}>#{val}</#{type}>"
}
# Now Ior realizes that empty lines will create
# <string></string>
# Ior needs to get rid of those..
p 'FINAL SOLUTION %s' % ['-' * 40]
data.each_line {|ln|
type = 'string'
val = ln.split('=')[1]
val.strip! if val
if val =~ /^\d*$/
type = 'integer'
elsif val =~ /^\d*\.\d*$/
type = 'float'
else
type = 'string' # Ior forgot it's already 'string'
end
puts "<#{type}>#{val}</#{type}>" unless val == '<string></string>'
}
# AH... it's not working!!! Why?
p 'TEST %s' % ['-' * 40]
data.each_line {|ln|
type = 'string'
val = ln.split('=')[1]
val.strip! if val
if val =~ /^\d*$/
type = 'integer'
elsif val =~ /^\d*\.\d*$/
type = 'float'
else
type = 'string' # Ior forgot it's already 'string'
end
if val != nil
puts "<#{type}>#{val}</#{type}>" unless val.chomp == '<string></string>'
end
}
# ..because of a ending new-line that can't be checked
# if the variable is nil.
# Just add another if...
# AHHHHH!!! It's WORKING!!!