From: "David A. Black" Date: 2004-03-04T20:58:08+09:00 Subject: Re: Why do inner methods count in outer scope? Hi -- On Thu, 4 Mar 2004, Tim Bates wrote: > Charles Comstock wrote: > > Out of curiosity, why don't you just use an eval to change function > > definitions? > > I do. Watch: > > def mk_func(:name) > eval <<-EOF > def #{name}(foo) > puts foo > end > EOF > end > > Even when using eval, you've got to use a def keyword somewhere to > define a method. You can use define_method, if you want to avoid both eval and def. Also, this gives you some control over scope, because you can put the new method in either the class of the object (so that other instances will be able to call it) or in the object's singleton class. class A def mk_func(n) self.class.class_eval { # or: (class << self; self; end).class_eval { define_method(n, proc {|foo| puts foo }) } end end (Yes, I know that class_eval is in the eval family :-) But you can avoid eval'ing strings this way.) David -- David A. Black dblack@wobblini.net