temporarily override instance variable in a block

J

Jean-denis Vauguet

Hi,

I strived to override instance variable on a per-block basis, but I'm
not satisfied with my results. Here's a snippet:
http://gist.github.com/589924

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!
 
B

Brian Candler

Jean-denis Vauguet said:
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
 
R

Robert Klemme

I strived to override instance variable on a per-block basis, but I'm
not satisfied with my results. Here's a snippet:
http://gist.github.com/589924

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top