String.intern vs String.to_sym

I

Ian Hunter

Can anyone make a case for ever using String.intern? It seems to be
nothing more than an alias to to_sym on String, and since it's not
defined on Symbol, you can't ducktype a var to get a symbol.

I'm trying to understand why this even exists in the Ruby language, it
doesn't seem like it would be useful for anything. variable.to_sym
seems to be the safer route to always use.

Thanks, ian
 
E

Eric Hodel

Can anyone make a case for ever using String.intern? It seems to be
nothing more than an alias to to_sym on String, and since it's not
defined on Symbol, you can't ducktype a var to get a symbol.

#intern came first, #to_sym is an alias of #intern.
I'm trying to understand why this even exists in the Ruby language, it
doesn't seem like it would be useful for anything. variable.to_sym
seems to be the safer route to always use.

It's useful for backwards compatibility.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top