From: "Albert L. Wagner" Date: 2002-01-19T04:32:43+09:00 Subject: dynamic method creation I have a need to dynamically create methods with method names that are not known until run time. Below is my current best shot. Is there a better way? If methods allowed expressions as names, and not just as qualifiers, I wouldn't need the eval. class Record def initialize(name) @name = name end end fieldNames = ['abc', 'dof', 'ghi'] fieldTypes = ['String', 'Fixnum', 'Float'] rec = Record.new('aRecord') class << rec end fieldNames.size.times do | i | f = fieldNames[i] defStr = "def rec.#{f}=(obj)\n" case fieldTypes[i] when 'String' then defStr << "@#{f} = obj\n" when 'Fixnum' then defStr << "@#{f} = obj\n" when 'Float' then defStr << "@#{f} = obj\n" end defStr << "end\n" puts defStr eval defStr end p rec.abc=(nil) p rec.dof=(nil) p rec.ghi=(nil) p rec.methods -- "It requires wisdom to understand wisdom: the music is nothing if the audience is deaf." - Walter Lippman