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.
Hmm… What version of Ruby are you using? You should be able to use
String#to_sym presently.
Originally the method for doing so was #intern. Though I haven’t heard
any specific verbage to the effect from offical sources, I would highly
recommend never using #intern again. Use #to_sym instead.
obviously its there IF you try to call it.
prblem on my side was that i looked in the docs http://www.ruby-doc.org/docs/ProgrammingRuby/
and there is no mention of #to_sym anywhere
so i thought its no available.
my fault
obviously its there IF you try to call it.
prblem on my side was that i looked in the docs Programming Ruby: The Pragmatic Programmer's Guide
and there is no mention of #to_sym anywhere
so i thought its no available.
my fault
I see. FYI for the future. The best place to check first is always
using ri:
$ ri to_sym
More than one method matched your request. You can refine
your search by asking for information on one of:
Fixnum#to_sym, String#to_sym, Symbol#to_sym
$ ri String#to_sym
…
T.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.