Struct and symbols

Folks,

I am new to Ruby and have a question about Struct.

I have something like

Record = Struct.new(:field1, :field2, :field3)

How can I make the symbol list flexible and go from, say,

s = [“field1”, “field2”, “field3”] to the above?

My objective is to generate the Record structure at runtime and use it
for a MySQL table adapter.

Thanks!

  • JP

That should work:

symbols = s.map{|f| f.intern}
Record = Struct.new(*symbols)

Regards,

Michael

···

On Thu, 2002-10-24 at 08:42, Jean-Paul wrote:

Folks,

I am new to Ruby and have a question about Struct.

I have something like

Record = Struct.new(:field1, :field2, :field3)

How can I make the symbol list flexible and go from, say,

s = [“field1”, “field2”, “field3”] to the above?

Hi,

Jean-Paul jp@vooght.de writes:

I have something like

Record = Struct.new(:field1, :field2, :field3)

How can I make the symbol list flexible and go from, say,

s = [“field1”, “field2”, “field3”] to the above?

Record = Struct.new(nil, *s)

···


eban