Temporarily override instance variable in a block

Hi,

I strived to override instance variable on a per-block basis, but I’m
not satisfied with my results. Here’s a snippet:

The goal is: execute a block but have it use a different value for a
previously defined instance variable, all of this in the context of a
class instance (the instance variable belongs to this class instance).

Two approach are illustrated: variable swapping and instance_eval. The
former does the trick but it’s cumbersome and not thread-safe. The
latter, I don’t understand why, fails, for the block seems to be called
in the

If anyone can help with this, thanks in advance!

Jean-denis Vauguet wrote:

Two approach are illustrated: variable swapping and instance_eval. The
former does the trick but it’s cumbersome and not thread-safe. The
latter, I don’t understand why, fails, for the block seems to be called
in the

If anyone can help with this, thanks in advance!

There is no general solution to this. It will depend on the actual
problem you’re trying to solve.

You’re right that replacing the instance variable is not thread-safe.
(Note that you should use begin … ensure … end to make sure the
variable is put back even in the case of an exception)

The self.class.new (or dup/clone) approach won’t work for all objects.
Some objects have compulsory arguments to their constructors, or have
all sorts of side effects in their ‘initialize’ methods. The nearest
you’re likely to get is:

o = self.class.allocate
instance_variables.each do |v|
o.instance_variable_set(v, instance_variable_get(v))
end

But this almost certainly means you’re thinking about the problem the
wrong way. Try a more functional approach: pass the proxy as an
argument, instead of looking at the @proxy instance variable. Or use the
instance variable as the default.

def whatever(proxy = @proxy)
… do something with proxy …
end

On Tue, Sep 21, 2010 at 7:06 PM, Jean-denis Vauguet [email protected]
wrote:

I strived to override instance variable on a per-block basis, but I’m
not satisfied with my results. Here’s a snippet:
as.rb · GitHub

The goal is: execute a block but have it use a different value for a
previously defined instance variable, all of this in the context of a
class instance (the instance variable belongs to this class instance).

Two approach are illustrated: variable swapping and instance_eval. The
former does the trick but it’s cumbersome and not thread-safe.

Exchanging instance variable values is never thread safe - unless
you take extra measures (synchronization).

The latter, I don’t understand why, fails, for the block seems to be called
in the

? Your second approach does not actually swap values of an instance
variable but tries to create a copy of the instance and work with
that. This has vastly different properties (apart from that it may
not work for certain types of objects as Brian said): in cases you
risk losing modifications on the copy.

I completely agree with Brian: please provide more background.

Kind regards

robert