Ugly First Crack at some Metaprogramming

Hey - this is pretty ugly and probably not even considered
Metaprogramming,
but have a look/laugh and please suggest a more elegant way:

Basically, I just want to loop through an array of variables to look for
in
the params hash and set them to instance variables with the same name or
set
them to ‘’ if non-existant in the hash:

[:criteria, :sort_by, :ord].each { |key| eval("@#{key} =
params[:#{key}] ||
‘’") }

all this does is just evaluate the following lines:

@criteria = params[:criteria]
@sort_by = params[:sort_by]
etc…

Better ideas?

Thanks

On Nov 29, 2007 12:09 PM, blinking bear [email protected] wrote:

all this does is just evaluate the following lines:

@criteria = params[:criteria]
@sort_by = params[:sort_by]

I’m not sure what this is buying you.

You might consider using Object#instance_variable_set instead of eval.