# String -\> symbol

**URL:** <https://www.ruby-forum.com/t/string-symbol/104813>\
**Category:** Ruby\
**Created:** [May 22, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/string-symbol/104813 "2007-05-22T21:14:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 22, 2007, 9:14pm UTC](https://www.ruby-forum.com/t/string-symbol/104813/1 "2007-05-22T21:14:22Z")

</div>

ok - i’m officially an idiot.

i want to dynamically alias the names of all methods in a module… i  
need to string-manipulate the original method name to get the new one.  
alias\_method only takes symbol names and the only tricks i know to  
convert strings to symbols (i.e., method(“this\_func”) and  
SomeClass.method\_eval("this\_func) don’t apply.

my\_module.instance\_modules.each{ |m|  
m\_new\_name =   
alias\_method \<what goes here?\> \<and what goes here?\>

more generally, is there some easy way to convert strings to symbols  
that i’ve missed?

thanks  
m

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 22, 2007, 9:19pm UTC](https://www.ruby-forum.com/t/string-symbol/104813/2 "2007-05-22T21:19:23Z")

</div>

On Wed, May 23, 2007 at 04:13:49AM +0900, removed\_email\_address@domain.invalid wrote:

> ok - i’m officially an idiot.
> 
> i want to dynamically alias the names of all methods in a module… i need to string-manipulate the original method name to get the new one. alias\_method only takes symbol names and the only tricks i know to convert strings to symbols (i.e., method(“this\_func”) and SomeClass.method\_eval("this\_func) don’t apply.
> 
> my\_module.instance\_modules.each{ |m|  
> m\_new\_name =   
> alias\_method \<what goes here?\> \<and what goes here?\>
> 
> more generally, is there some easy way to convert strings to symbols that i’ve missed?

“a\_string”.to\_sym # =\> :a\_string

enjoy,

-jeremy

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 22, 2007, 9:22pm UTC](https://www.ruby-forum.com/t/string-symbol/104813/3 "2007-05-22T21:22:23Z")

</div>

From: removed\_email\_address@domain.invalid [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]  
Sent: Tuesday, May 22, 2007 10:14 PM

> more generally, is there some easy way to convert strings to symbols that  
> i’ve missed?

Is String#to\_sym (or String#intern) what you looking for?

V.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 22, 2007, 11:02pm UTC](https://www.ruby-forum.com/t/string-symbol/104813/4 "2007-05-22T23:02:34Z")

</div>

> > more generally, is there some easy way to convert strings to symbols  
> > that i’ve missed?
> 
> Is String#to\_sym (or String#intern) what you looking for?

Which one is in fact preferred?

- donald

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 22, 2007, 9:36pm UTC](https://www.ruby-forum.com/t/string-symbol/104813/5 "2007-05-22T21:36:24Z")

</div>

removed\_email\_address@domain.invalid wrote:

> alias\_method only takes symbol names  
> That isn’t true. alias\_method takes strings just fine:

> > class A; alias\_method ‘foo’, ‘to\_s’;end; A.new.foo

# =\> “#\<A:0xb7cfcf08\>”

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 23, 2007, 12:07am UTC](https://www.ruby-forum.com/t/string-symbol/104813/6 "2007-05-23T00:07:39Z")

</div>

removed\_email\_address@domain.invalid wrote:

> more generally, is there some easy way to convert strings to symbols that  
> i’ve missed?

String#to\_sym

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [May 23, 2007, 12:14am UTC](https://www.ruby-forum.com/t/string-symbol/104813/7 "2007-05-23T00:14:39Z")

</div>

On 5/22/07, Ball, Donald A Jr (Library) [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > more generally, is there some easy way to convert strings to symbols  
> > > that i’ve missed?
> > 
> > Is String#to\_sym (or String#intern) what you looking for?
> 
> Which one is in fact preferred?

Either one, they are synonymous.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
