Implementation of Binding#[]= and #[]


(Mauricio Fernndez) #1

IIRC the RCR for Binding#[]= and Binding#[] was accepted a long time ago,
but I’ve never seen it implemented. Of course it’d be better to see it
done in C, but in the meantime…

class Binding
def []=(name, val)
var = "_magic#{Time.new.to_i}#{rand(100000)}"
setter = eval(“proc {|#{var}| #{name} = #{var}}”, self)
setter.call(val)
val
end

def [](name)
    eval(name, self)
end

end

Comments?

I have failed to find ‘prior art’ for this ;), but I don’t think I’m
the first one to think of it.

···


_ _

__ __ | | ___ _ __ ___ __ _ _ __
’_ \ / | __/ __| '_ _ \ / ` | ’ \
) | (| | |
__ \ | | | | | (| | | | |
.__/ _,
|_|/| || ||_,|| |_|
Running Debian GNU/Linux Sid (unstable)
batsman dot geo at yahoo dot com

Sorry. I just realized this sentance makes no sense :slight_smile:
– Ian Main