Tim Hunter
7/1/2005 10:58:00 PM
Joe Van Dyk wrote:
> I thought for all of five seconds for a good subject line for this
> question, but failed. Sorry!
>
> I have a string like:
>
> "some_key: blah, some_other_key: more_blah, yet_other_key: yet_more_blah"
>
> I want to build up a hash like
>
> { :some_key => "blah", :some_other_key => "more_blah", :yet_other_key
> => "yet_more_blah" }
>
> And I don't really want to have to know what the possible keys are in advance.
> So, the message format looks like:
> <key>: <value>, <key>: <value>
>
> How can I properly extract it out?
>
> Here's my initial attempt, which works, but seems hackish:
>
> attributes = message.split(",")
> attributes.each do |attribute|
> key, value = attribute.scan(/(\w+): (.+)/)[0]
> result_hash[key.to_sym] = value.strip
> end
>
> Also, this will get ran potentially thousands of times per second, so
> executation speed is of some concern.
>
>
It doesn't look particularly hackish to me, but maybe my sensibilities
aren't fine enough. The only thing I'd say is that if performance is
important then we ought to ask the regular expression to strip
whitespace around the key and value so we can avoid the #strip method.
Here's my version:
require 'pp'
hash = Hash.new
DATA.each do |line|
attrs = line.split(/,/)
attrs.each do |attr|
m = /\s*(\w+)\s*:\s*(\w+)\s*/.match(attr)
raise "#{attr.chomp} doesn't look like key:value" unless m
hash[m[1].intern] = m[2]
end
end
pp hash
__END__
some_key: blah, some_other_key: more_blah, yet_other_key: yet_more_blah
key1:value1
key2: value2
key3 : value 3 , key4 :value4 , key555555555:value55555
The output is:
{:some_other_key=>"more_blah",
:key555555555=>"value55555",
:yet_other_key=>"yet_more_blah",
:key1=>"value1",
:key2=>"value2",
:key3=>"value",
:some_key=>"blah",
:key4=>"value4"}