On 5/20/05, johng <jgoalby / gmail.com> wrote: > Hi, > > In message "Re: Anonymous module undefined method error" > on Sat, 21 May 2005 06:45:15 +0900, "johng" <jgoa... / gmail.com> > writes: > > | > > I am new to Ruby so please excuse my question. I did search > |usenet for an answer but couldn't find one: > | > |I have a variable, data, that was read from a file and has the > |following contents: > | > |def foo > | return "foo" > |end > | > |My code is this: > | > |amodule = Module.new > |amodule.class_eval(data) > | > |puts(amodule.public_instance_methods) -> outputs "foo" > | > |amodule.foo -> fails saying method is > undefined > > |A public *instance* method means methods are available for their > |instances, "amodule" is a module, not an instance. When you include a > |module to a class, and instantiate from the class, you can call "foo". > | > | matz. > > Ah, ok, I was fooled by the Module.new. I had assumed that it made > an instance. > > So, what I wanted to do, was have a class defined in a file and call > a known method on it. For instance: > > class Bar > def Bar.foo > return "foo" > end > end > > But I will not know the class name contained in the file (Bar). I > tried > the approach from the original post so that I could call a defined > method that would instantiate the class for me. I believe I can do > this now relatively easily. It does though put more requirements > on the file. > > I wonder though, if there is a cleaner way? > > Thanks, > > John. > > > more requirements on the file? you can do the same thing, you just have to create an instance: -bash: [~]$ cat testdata def foo "foo!" end -bash: [~]$ cat test.rb data = File.read( "testdata" ) aclass = Class.new aclass.class_eval( data ) a = aclass.new puts a.foo -bash: [~]$ ruby test.rb foo! -- ---------- Please do not send personal (non-list-related) mail to this address. Personal mail should be sent to polyergic / sterfish.com.