Brian Schröäer <ruby.brian / gmail.com> wrote:
> On 02/10/05, Robert Klemme <bob.news / gmx.net> wrote:
>> Brian Schröäer <ruby.brian / gmail.com> wrote:
>>> [snip]
>>
>> Just don't use the block for define_method but invoke it from the
>> closure:
>>
>> class A
>>   def self.def_me(cmd,&block)
>>     define_method(cmd) do
>>       puts "preparation"
>>       instance_eval &block
>>     end
>>   end
>>
>>   attr_accessor :name
>> end
>>
>> a=A.new
>> a.name = "Einstein"
>>
>> A.def_me(:foo) do
>>   puts @name, self.name
>> end
>>
>>>> a.foo
>> preparation
>> Einstein
>> Einstein
>> => nil
>>
>> Kind regards
>>
>>     robert
>>
>>
>>
>
> thanks a lot thats perfect. I have to wrap my mind around all the
> different evals and definition methods and have them at hand when
> needed. Theres a long way to go.

They are not really too complicated IMHO: instance_eval is a variant of eval 
that allows a string and a block and just rebinds "self" before evaluating 
the string / block.  class_eval is an extension of instance_eval which makes 
sure that any "def"'s do not apply to the instance itself but to all 
instances of the class.  eval is the basic method that accepts only a string 
but can work with any binding.

Kind regards

    robert