String.to_sym?

R

robertj

hi,

is there a String#to_sym method anywhere in the
statndard lib? i couldnt find anything like that
but maybe i missed it.

its easy enough to write

class String
def to_sym
eval ":#{self}"
end
end

just want to check if its there already.

ciao robertj
 
N

nobu

Hi,

At Tue, 13 Dec 2005 00:27:38 +0900,
Mark J.Reed wrote in [ruby-talk:170247]:
It's called "intern", because Ruby internalizes the string into a symbol.

String#to_sym is also an alias in 1.8 or later.
 
T

Trans

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.

T.
 
T

Trans

robertj said:
hi,

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 :)

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top