Joe Van Dyk wrote: >Can someone help me make this code not suck? > >require 'test/unit' > ># EWWWWWWW >def process_string str > result = Hash.new > str.to_a.each do |line| > line.scan(/important ([a-zA-Z0-9]+): /) do |key| > result[key.first] = line > end > end > result >end > > def process_string str result = {} str.each_line do |line| important_stuff = line.split(/important\s+/,2)[1] or next key, value = important_stuff.split ':' result[key] = value end result end # More readable? I dunno... you be the judge. I might prefer it over: # line, key, value = line.match /important\s+(\w+):\s*(.*)$/ # Though the latter is much more explicit. >class TestThis < Test::Unit::TestCase > def example_string > <<-END > # comments > important key1: some value's here > important key2: some value's here > important key3: some value's here > important key4: some value's here > > other stuff we don't care about > END > end > > def test_process_string > result = process_string example_string > assert 4, result.size > assert result.has_key?("key1") > assert result.has_key?("key2") > assert result.has_key?("key3") > assert result.has_key?("key4") > end >end > >