ale / crimson.propagation.net wrote:
>On Thu, 25 Jan 2001, Kevin Smith wrote:
>> Hmmm....my first cut (in pseudocode) would be:
>> for each line
>>   if it's a short parameter
>>      process that parameter
>>   else
>>      memorize the terminator
>>      read lines until you hit the terminator
>>      process that parameter
>>   end
>> end
>
>Kevin, one comment on your style. I tend to outline my routines in a
>same way, and I guess it's a good thing to do. But if you pay
>attention to outlining style, then you have half-baked Ruby code
>already.

In real life, I very rarely write pseudocode like 
this. I would start writing tests, and then fill 
in the code. In this case, rather than take the 
time to write real code, or to post untested 
Ruby(ish) code, I figured pseudocode was the way 
to go.

>  def read(file)
>    new_param_re = /^[^\s]/
>    for line in file
>      next if line.strip == ""  # skip empty rows
>
>      value = line
>
>      name, value = line.split(/\s/, 2) if line =~ new_param_re

Argh. This formatting drives me insane. I want it 
to be really obvious that name and value may or 
may not be assigned. I would much prefer:

	if line =~ new_param_re
		name, value = line.split(/\s/, 2)
	else
		value = line
	end

>
>      if @params.has_key? name
>	value = "\n" + value   # if appending, add new lines too
>      else
>	@params[name] = ""     # if not, initialize hash
>      end
>
>      # remove leading space (only one) and trailing new line if any
>      @params[name] += value.chomp.sub(/^ /, "") 
>    end
>  end    

Otherwise, that approach seems reasonable. The 
function is short enough not to raise my 
eyebrows. I think my pseudocode might lend itself 
to breaking out a function to do the stuff in the 
middle, rather than relying on 'name' persisting 
from one line to the next. But either way works.

Kevin