Well, that just makes it even *more* like Lisp than I was thinking. It hadn't occurred to me
that Ruby creates a symbol for a variable. But I guess it makes sense given some code I've seen that uses symbols to name methods and attributes and the like. assigning b = :a is like doing (setf b 'a) in Lisp, b contains a symbol, not 1 or a reference to the variable a itself or anything like that. What I wonder then, is what happens in Ruby if you were to then call eval(b) ? I haven't tried it.
--ch--
···
-------------- Original message ----------------------
From: dblack@wobblini.net
Ruby symbols aren't exactly used as variable names, though. If you
assign to a variable:
a = 1
then Ruby creates and stores an :a symbol. But if you use the symbol
:a, like this:
b = :a
there's no direct semantic connection to your variable called 'a'.
Hi --
From: dblack@wobblini.net
Ruby symbols aren't exactly used as variable names, though. If you
assign to a variable:
a = 1
then Ruby creates and stores an :a symbol. But if you use the symbol
:a, like this:
b = :a
there's no direct semantic connection to your variable called 'a'.
Well, that just makes it even *more* like Lisp than I was thinking. It hadn't occurred to me
that Ruby creates a symbol for a variable. But I guess it makes
sense given some code I've seen that uses symbols to name methods
and attributes and the like. assigning b = :a is like doing (setf b
'a) in Lisp, b contains a symbol, not 1 or a reference to the
variable a itself or anything like that. What I wonder then, is
what happens in Ruby if you were to then call eval(b) ? I haven't
tried it.
eval wants a string:
eval(b)
TypeError: can't convert Symbol into String
I think a case could be made for a Symbol#to_str method, since there
can't be anything that eval(sym) would mean except to eval a string
representation of the symbol, and same for appending symbols to
strings. But I don't know whether there are hidden complications I'm
not thinking of.
David
···
On Sat, 29 Jul 2006, nothinghappens@mchsi.com wrote:
-------------- Original message ----------------------
--
http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
Ruby for Rails => book, Ruby for Rails
http://www.rubycentral.org => Ruby Central, Inc.