Procs and scope (function pointers)

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?

Thanks

···


Jim Freeze
If only I had something clever to say for my comment…
~

Jim Freeze jim@freeze.org writes:

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

Is there a way to access instance methods/variables and still
keep the function definitions at the class level?

You could try

def compute(x)
instance_eval(&func)
end

Cheers

Dave

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

···

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?

Jim Freeze jim@freeze.org writes:

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

Is there a way to access instance methods/variables and still
keep the function definitions at the class level?

You could try

def compute(x)
instance_eval(&func)
instance_eval(&func(x)) # would I pass the argument like this?

···

On Tue, Jul 30, 2002 at 10:30:29PM +0900, Dave Thomas wrote:

end


Jim Freeze
If only I had something clever to say for my comment…
~

Yes, I think that is what I want.

···

On Wed, Jul 31, 2002 at 01:18:48AM +0900, Pit Capitain wrote:

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

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


Jim Freeze
If only I had something clever to say for my comment…
~

Hi,

···

At Wed, 31 Jul 2002 01:31:43 +0900, Jim Freeze wrote:

You could try

def compute(x)
instance_eval(&func)
instance_eval(&func(x)) # would I pass the argument like this?
end

No, it means calling func with an argument and its result would
be passed to instance_eval as a block. It might not be what
you expect.

There was proposals to add arguments to instance_eval or add a
new method instance_yield in ruby-dev and ruby-talk.


Nobu Nakada