Hello -- On Sun, 15 Jul 2001, HarryO wrote: [Michael wrote:] > > Try eval: > > > > methName = "myMethod" > > a = "test" > > eval %{ > > def #{ methName } > > ... > > end > > } > > Yep; that should work. What if I want to create a global function? I > assume I can use "class << SOMETHING", but I'm not sure what the > SOMETHING would be. > > I'm guessing that if I put that code outside any classes then the eval > will create a global function. I'll try that out. At any point (inside or outside a class definition), you could do: meth = "talk" # using string interpolation just to illustrate output = "hi" Object.module_eval <<-EOE def #{meth} puts "#{output}" end EOE > > Another possible solution is: > > > > class X > > def create > > def self.myMethod > > p "in myMethod" > > end > > end > > end > > Yep. I'd thought of that, but the functions are going to be created for > arbitrary classes. You can always switch to the context of a particular class, even if you just have its name in a string: class Thing end class Other c = "Thing" m = "greet" cc = eval "#{c}" cc.class_eval <<-EOE def #{m} puts "Hello" end EOE end Thing.new.greet # => Hello or some variant thereof. (Q: is there another way, other than the brute force eval, to get a class object from a string?) > If I can make this work, I'll be a very happy boy. At the moment, I feel > like my chances are about 50-50 :-). This is Ruby. The odds are greatly in your favor :-) David -- David Alan Black home: dblack / candle.superlink.net work: blackdav / shu.edu Web: http://pirate.shu.edu/~blackdav