Forum: Ruby String.to_sym?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D67ca51e6025a5ed5282205e70379798?d=identicon&s=25 robertj (Guest)
on 2005-12-12 16:15
(Received via mailing list)
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
Ec2a9a9da5c8a5f14a0fe2361ae4e08a?d=identicon&s=25 Mark J.Reed (Guest)
on 2005-12-12 16:30
(Received via mailing list)
"robertj" <robert_kuzelj@yahoo.com> writes:

>hi,

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

It's called "intern", because Ruby internalizes the string into a
symbol.
And it's rather more efficient than the eval method you propose.

irb(main):001:0> "somestring".intern
=> :somestring
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 unknown (Guest)
on 2005-12-12 16:36
(Received via mailing list)
Hi,

At Tue, 13 Dec 2005 00:27:38 +0900,
Mark J.Reed wrote in [ruby-talk:170247]:
> >is there a String#to_sym method anywhere in the
> >statndard lib? i couldnt find anything like that
> >but maybe i missed it.
>
> It's called "intern", because Ruby internalizes the string into a symbol.

String#to_sym is also an alias in 1.8 or later.
912c61d9da47754de7039f4271334a9f?d=identicon&s=25 unknown (Guest)
on 2005-12-12 19:07
(Received via mailing list)
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
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Trans (Guest)
on 2005-12-12 19:28
(Received via mailing list)
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.
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2005-12-12 21:07
(Received via mailing list)
mental@rydia.net writes:

>
>> its easy enough to write
>>
>> class String
>>     def to_sym
         eval ":#{dump}"
D67ca51e6025a5ed5282205e70379798?d=identicon&s=25 robertj (Guest)
on 2005-12-13 11:33
(Received via mailing list)
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 :-)

ciao robertj
45196398e9685000d195ec626d477f0e?d=identicon&s=25 Trans (Guest)
on 2005-12-13 15:43
(Received via mailing list)
robertj wrote:
> 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.
This topic is locked and can not be replied to.