MenTaLguY
12/12/2005 6:04:00 PM
Quoting robertj <robert_kuzelj@yahoo.com>:
> hi,
>
> is there a String#to_sym method anywhere in the
> statndard lib? i couldnt find anything like that
> but maybe i missed it.
If you've got a reasonably recent version of Ruby, yes. I don't
know about 1.6 or earlier.
> its easy enough to write
>
> class String
> def to_sym
> eval ":#{self}"
> end
> end
If you had to write one yourself, you'd want:
class String
alias to_sym intern
end
eval'ing ":#{self}" won't work if the string has spaces or anything
like that. In general, eval'ing strings should be an absolute last
resort in code, because it is very difficult to make reliable.
-mental