From: Max Russell Date: 2006-11-24T19:00:05+09:00 Subject: Re: Metaprogammatically define methods. Suraj Kurapati wrote: > Suraj Kurapati wrote: >> class Foo >> File.open('readme.txt').readlines.each do |entry| > > Well it turns out that the above line can be simplified to: > > File.open('readme.txt').each_line do |entry| > > or even to: > > IO.foreach('readme.txt') do |entry| > > Personally, I prefer each_line because IO.foreach sounds like "for each > IO object in ..." instead of "for each line in ...". > >> define_method "test_#{entry}".to_sym do >> puts "testing #{entry}" >> end >> end >> end >> >> Foo.new.test_whatever Got a bit of help on this one and the solution I'm now going with is: File.open('readme.txt').each_line do |entry| self.send(:define_method, entry.strip.to_sym){ puts "testing #{entry}" } #self.send(:public, entry.strip.to_sym ) end end -- Posted via http://www.ruby-forum.com/.