eval/binding question

R

Robert Klemme

Stefan Kaes said:
No. _code gets parsed on every call to local_eval.

If you want to avoid that you'll need a two step approach. But then the
var names are needed beforehand... Ok, I see the problem (finally) :)

Cheers

robert
I omitted the template name and the value as these are just special cases
of values with fixed names. But you get the picture. And there is no
reparsing needed.

def local_eval(_values, _code)
_val = nil
_bnd = binding
p local_variables
_values.each {|name, val| eval "#{name}=_val", _bnd}
p local_variables
eval _code, _bnd
end


local_eval( {:x=>1}, "puts x" )
["_values", "_code", "_val", "_bnd"]
["_values", "_code", "_val", "_bnd", "x"]
nil
=> nil
-- stefan
 
E

Eric Hodel

--Apple-Mail-4--455575283
Content-Transfer-Encoding: 7bit
Content-Type: text/plain; charset=US-ASCII; format=flowed

Why did you lie???

I didn't bother to double-check first. My bad.
Thanks for the code, but I already knew this would also be a possible
solution. But probably less efficient than using instance variables,
though I have not checked that yet. Did you?

attr* methods are optimized for speed so you don't have all the
overhead of a regular method call, but I doubt this will be faster than
ivars.

An ivar implementation would be pretty easy too, use
instance_variable_set instead of send.

Here's how Ruby optimizes attr* methods:

$ parse_tree_show
class Example
attr_reader :x
def y() @y; end
end
[[:class,
:Example,
:Object,
[:defn, :x, [:ivar, :mad:x]],
[:defn, :y, [:scope, [:block, [:args], [:ivar, :mad:y]]]]]]

No scope setup is done for attr*.

--
Eric Hodel - (e-mail address removed) - http://segment7.net
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

--Apple-Mail-4--455575283
content-type: application/pgp-signature; x-mac-type=70674453;
name=PGP.sig
content-description: This is a digitally signed message part
content-disposition: inline; filename=PGP.sig
content-transfer-encoding: 7bit

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (Darwin)

iD8DBQFCOHKbMypVHHlsnwQRAmJCAJ4oiwEylvrmGisLFcgO418Y1FZnVgCfQoo7
m0X3hNlnq4YO4L7nnKMLHiQ=
=juic
-----END PGP SIGNATURE-----

--Apple-Mail-4--455575283--
 
S

samuel_x_winters_x

+1

I have on a couple of occasions also been bit by this. It would be much
nicer (to the programmer, at any rate) for
code and eval "code"
to behave consistently with each other.

In particular, manipulating variable bindings is extremely difficult
without.

So I'd like to revive this request, if possible ...
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top