Reputation:
Say I have a string as follows:
"auto: true; server: false;"
...and I want a regular expression to create a hash of these settings. I have the below code:
# class Configurer...
def spit(path = "", *args)
spat = Hash.new
if File.file?(path)
# Parse file
else
args.each do |arg|
begin
if path.include? arg + ":"
strip = path.match(/#{arg}:\s(.*);/)
spat[arg] = strip[1]
end
rescue
return "Error when parsing '#{arg}' in direct input."
end
end
end
spat
end
When something like:
config = Configurer.new
puts config.spit("auto: true; server: false;", "auto", "server")
...is ran, the output is an incorrect hash of:
# => {"auto"=>"true; server: false", "server"=>"false"}
Why is that? When I parse a file (line by line) and use the same regular expression I get the desired hash. Why is this not the case with this method?
Upvotes: 0
Views: 63