I now propose this as a more straight-forward solution to the problem.

class Foo

    # Each instance will have its own version of method "bar".
    def initialize(&block)
       @block = block
       def self.bar(*args)
          @block.call(*args)
       end
    end

end

a = Foo.new {puts "foobar"}
b = Foo.new {|s| printf "%s %s\n", s, s}
c = Foo.new {|m, n| p m + n}
a.bar  => foobar
b.bar("foobar") => foobar foobar
c.bar(2, 3) => 5

Regards, Morton

On Jul 18, 2006, at 7:52 AM, Chris Roos wrote:

> Ok, so it seems to be with scoping and my choice of the proc  
> variable name.
>
> Anything other than a variable named proc will cause the more  
> understandable
>
> => NameError: undefined local variable or method `prc' for
> #<Class:#<Foo:0x342054>>
>
> Chris
>
> On 7/18/06, Chris Roos <chrisjroos / gmail.com> wrote:
>> Hi,
>>
>> In both examples below, the expected results are observed.  However,
>> in the second example (class Bar) warnings are generated.  This is
>> Ruby 1.8.2 on Mac Os x 10.4.7.
>>
>> I wonder if someone might be able to enlighten me?
>>
>> Chris
>>
>> class Foo
>>   def initialize(&proc)
>>     meta = class << self; self; end
>>     meta.send(:define_method, :bar, &proc)
>>   end
>> end
>>
>> foo = Foo.new { false }
>> puts foo.bar #=> false
>> foo = Foo.new { 'hello world' }
>> puts foo.bar #=> hello_world
>>
>> class Bar
>>   def initialize(&proc)
>>     class << self
>>       define_method(:foo, &proc)
>>     end
>>   end
>> end
>>
>> bar = Bar.new { false }
>> #=> warning: tried to create Proc object without a block
>> puts bar.foo #=> false
>> bar = Bar.new { 'hello_world' }
>> #=> warning: tried to create Proc object without a block
>> puts bar.foo #=> hello_world
>>