RosettaCodeData/Task/Read-a-configuration-file/Ruby/read-a-configuration-file.rb

26 lines
743 B
Ruby
Raw Permalink Normal View History

2013-10-27 22:24:23 +00:00
fullname = favouritefruit = ""
needspeeling = seedsremoved = false
otherfamily = []
2013-04-10 23:57:08 -07:00
2013-10-27 22:24:23 +00:00
IO.foreach("config.file") do |line|
2013-04-10 23:57:08 -07:00
line.chomp!
key, value = line.split(nil, 2)
case key
when /^([#;]|$)/; # ignore line
when "FULLNAME"; fullname = value
when "FAVOURITEFRUIT"; favouritefruit = value
when "NEEDSPEELING"; needspeeling = true
when "SEEDSREMOVED"; seedsremoved = true
2013-10-27 22:24:23 +00:00
when "OTHERFAMILY"; otherfamily = value.split(",").map(&:strip)
2013-04-10 23:57:08 -07:00
when /^./; puts "#{key}: unknown key"
end
end
2013-10-27 22:24:23 +00:00
puts "fullname = #{fullname}"
2013-04-10 23:57:08 -07:00
puts "favouritefruit = #{favouritefruit}"
2013-10-27 22:24:23 +00:00
puts "needspeeling = #{needspeeling}"
puts "seedsremoved = #{seedsremoved}"
otherfamily.each_with_index do |name, i|
puts "otherfamily(#{i+1}) = #{name}"
end