In article <J%Hb6.6735$wz.209253 / nntp1.chello.se>, Niklas Backlund wrote:
>The "main loop" in the method that reads the file, reads
>a parameter on each line (for simplicity) according to a
>keyword-value scheme, and constructs the appropriate
>objects. However, some of the data I want to read are
>strings containing line breaks. I solved it by doing
>something along the lines of here documents, i e
>something like
>
>parameter value
>parameter value
>long-parameter WHATEVER
>  data
>  data
>WHATEVER
>parameter value
>...
>
>So I'd like the main loop to read normal parameters, and
>when it encounters a long parameter-type, let another
>method copy a few lines "verbatim", and when it's done,
>assign the data to the appropriate instance variable and
>let the main loop pick up after the end marker as if
>nothing happened...

Do you mean that the main loop would have to know which parameters are
long and normal? It seems better to encode this in the file.

In my opinion, rolling your own parser for a config file is seldom a good
solution. It is better to use an existing parser that provides a familiar
syntax, supports comments, can report about errors, etc.

In ruby, we can use the ruby parser itself. One way is to write your config
file as.

	param1 = "value1"
	param2 = "value2"
	param3 = <<PARAM3
	A
	long
	value
	PARAM3

And use it as:

	def read_config(file)
		eval(IO::File.new(file).read)
		binding
	end

	params = read_config("config")
	p eval "param1", params
	p eval "param3", params

Note that you should only use this method if the person who writes the
config file is trusted, since that person can specify arbitrary code for
the program to execute.

// Niklas