On 30 Jul 2002, at 21:48, Jim Freeze wrote:

> Hi:
> 
> I have a class that that has several 
> procs (function pointers). I would like to 
> keep these procs defined as class variables.
> Also, I wish the procs to be able to access member
> variables. However, I'm not sure this is possible.
> 
> I did see some esoteric tricks by Matz in earlier postings
> but I did not know if they apply.
> 
> Here is a sample of what I want to do:
> 
> 
> class Func
>   F1 = proc { |x| x }
>   F2 = proc { |x| x**2 }
>   ...
> 
>   def initialize(func)
>     case func
>       when :F1 then @func = F1
>       when :F2 then @func = F2
>       ...
>       raise "unknown"
>   end
> 
>   def compute(x)
>      @func.call(x)
>   end
> end
> 
> This works, but not if F? accesses an instance  variable or method. If
> I move the proc definitions inside the initialization method, I can
> get the required binding I need and can call instance methods.
> 
> Is there a way to access instance methods/variables and still
> keep the function definitions at the class level?

Hi Jim,

could you tell us why you want to use Proc objects in class 
constants? Couldn't you just use methods, as in

  class Func
    def F1( x ) x end
    def F2( x ) x**2 end
    ...

    def initialize( func )
      if func.to_s =~ /^F\d+$/
        @func = method( func )
      else
        raise "unknown"
      end
    end

    def compute(x)
       @func.call(x)
    end
  end

Regards,
Pit