local_variable_set ?

H

Hal Fulton

No, this isn't April Fool's Day. I'm seriously wondering
if this has ever been considered.

I can only think of one place I'd use it -- to turn hashes
(used as named parameters) into real variables.

Something like:

def meth(hash)
bind = binding
hash.each_pair {|k,v| local_variable_set(k,v,bind) }
# Assuming call was: meth(alpha: 123, beta: 234, gamma: 345)
# we now can reference alpha, beta, and gamma as ordinary
# variables...
end


Thoughts?


Hal
 
D

David A. Black

Hi --

No, this isn't April Fool's Day. I'm seriously wondering
if this has ever been considered.

I can only think of one place I'd use it -- to turn hashes
(used as named parameters) into real variables.

Something like:

def meth(hash)
bind = binding
hash.each_pair {|k,v| local_variable_set(k,v,bind) }
# Assuming call was: meth(alpha: 123, beta: 234, gamma: 345)
# we now can reference alpha, beta, and gamma as ordinary
# variables...
end

I think making bindings writeable has been suggested:

binding[:alpha] = 123

or whatever.
Thoughts?

To me it suggests too tight a coupling between the caller and the
method. If you write a method that depends on being told to create a
local variable with a particular name, you're limiting what can be
done by way of refactoring and reimplementing the method.


David
 
D

David A. Black

Hi --

Hi --

No, this isn't April Fool's Day. I'm seriously wondering
if this has ever been considered.

I can only think of one place I'd use it -- to turn hashes
(used as named parameters) into real variables.

Something like:

def meth(hash)
bind = binding
hash.each_pair {|k,v| local_variable_set(k,v,bind) }
# Assuming call was: meth(alpha: 123, beta: 234, gamma: 345)
# we now can reference alpha, beta, and gamma as ordinary
# variables...
end

I think making bindings writeable has been suggested:

binding[:alpha] = 123

or whatever.
Thoughts?

To me it suggests too tight a coupling between the caller and the
method. If you write a method that depends on being told to create a
local variable with a particular name, you're limiting what can be
done by way of refactoring and reimplementing the method.

Maybe not, though. I guess the idea would be that the caller is
supposed to send those names in a hash anyway. I think I might find
it weird, though, to see:

def meth(hash)
bind = binding
hash.each_pair {|k,v| local_variable_set(k,v,bind) }
puts alpha
beta += 2
if defined?(gamma)
puts "gamma came from somewhere..."

etc., without having any visual cues (arglist or assignment) as to
where those names came from.


David
 
J

Jeremy Kemper

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

No, this isn't April Fool's Day. I'm seriously wondering
if this has ever been considered.

I can only think of one place I'd use it -- to turn hashes
(used as named parameters) into real variables.

Stefan Kaes had a similar request several months ago:

http://groups.google.com/group/comp.lang.ruby/browse_thread/thread/
a954f8aaf698a0b9

Regards,
jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDSgIWAQHALep9HFYRAlpcAJ9LWKCPTC9xNo48mabidEsfShT8PACgw2Fb
AzDR1PMb5LfrhCCmBMTaW0o=
=9z7U
-----END PGP SIGNATURE-----
 
M

Mauricio Fernández


The eval("lambda{|x| #{lvar} = x}", somebinding).call(val) trick won't
allow you to create new locals and refer to them normally. Taking the
example from the OP:

# hash is {:alpha => 1, :beta => 2, :x => 0}

alpha = beta = nil # you can't get rid of this in the current implementation

bind = binding
hash.each_pair {|k,v| local_variable_set(k,v,bind)
# we can use alpha & beta normally, but x requires eval("x")
 
D

Devin Mullins

Mauricio said:
The eval("lambda{|x| #{lvar} =3D x}", somebinding).call(val) trick won't
allow you to create new locals and refer to them normally.
Point.
 

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

Forum statistics

Threads
474,184
Messages
2,570,975
Members
47,533
Latest member
medikillz39

Latest Threads

Top