From: 7stud -- Date: 2007-11-24T03:09:18+09:00 Subject: Re: New at regexp and Ruby need help on parsing a string. Gabra Kadabra wrote: > Raul Parolari wrote: >> >> str.scan(/ (and|not) (.*?) (?= \band|\bnot|$) /x) do |k, v| >> smoking_table[k.to_sym].push(v.strip) >> end > > I think Raul just convinced me that I really need to start a deep > relationship with regexp. > This is magic in one row, readable in three. > Don't be fooled by one liners. Ruby syntax allows you to string multiple method calls together in a compact way--yet the result can be very inefficient. Whenever I see a one liner with multiple method calls strung together and regex's sprinkled in for good measure, I immediately assume there is a more efficient solution. The solution I posted is a case in point: even though it has five times the number of lines, it is 70% faster on my system than the one liner you find so alluring. In addition, I find one liners hard to decipher, and since I don't aspire to write hard to read code that is also inefficient, I rarely try to cram a whole program into a single line. >>Peter Vanderhaden wrote: >> >> I used puts smoking_table. I'm assuming that's not the correct >> way to do it. Use the p command instead of puts to get the nice dictionary format. -- Posted via http://www.ruby-forum.com/.