Hi,
In message "Symbols: More Functionality Wanted"
on 03/01/20, Ryan Pavlik <rpav / nwlink.com> writes:
|I've been discussing this for a bit on #ruby-lang on OPN (or freenode or
|whatever), and am going to post it here, per suggestion.
|
|Basically, the Symbol class should have more useful functionality. One
|should not have to do the following:
|
| @a = 1
| str = "@a"
| val = eval(str)
|
|Especially when we've got a perfectly wonderful Symbol class which is
|not only there, but used internally for just this purpose. I'd like to
|see the following:
|
| @a = 1
| sym = :@a
| val = sym.binding # Or whatever... pick your favorite name ;)
|
|More importantly:
|
| @a = 1
| sym = :@a
| sym.bind(2)
| p @a # => 2
|
|This has multiple advantages:
|
| * It's more efficient.
| * eval() sucks anyway. :-)
| * It allows rebinding variables dynamically
| * Symbols become useful in ruby code
| * All the internal calls are already there... like rb_ivar_set,
| rb_ivar_get, and these all take IDs (which can be taken directly
| from the symbol)
Interesting idea, but I feel like they are responsibility of Binding,
not Symbol. For example,
b = binding()
b.bind(:a, 2)
b.value(:a)
matz.