# String to symbol

**URL:** <https://www.ruby-forum.com/t/string-to-symbol/119367>\
**Category:** Ruby\
**Created:** [November 6, 2007, 1:28pm UTC](https://www.ruby-forum.com/t/string-to-symbol/119367 "2007-11-06T13:28:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fernan2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fernan2](https://www.ruby-forum.com/u/fernan2)\
**Post date:** [November 6, 2007, 1:28pm UTC](https://www.ruby-forum.com/t/string-to-symbol/119367/1 "2007-11-06T13:28:53Z")

</div>

Hi people,

Is there a way to “intern” a string so that it becomes denoted by a  
symbol?

Something like:

# This doesn’t work as I wrote it, I know

:repetitive\_message \<= “blah blah blah”

puts :repetitive\_message  
puts :repetitive\_message  
puts :repetitive\_message  
puts :repetitive\_message

TIA

---

<div class="post-metadata">

**Author:** ![fernan2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fernan2](https://www.ruby-forum.com/u/fernan2)\
**Post date:** [November 6, 2007, 1:34pm UTC](https://www.ruby-forum.com/t/string-to-symbol/119367/2 "2007-11-06T13:34:25Z")

</div>

try  
to\_sym

---

<div class="post-metadata">

**Author:** ![fernan2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fernan2](https://www.ruby-forum.com/u/fernan2)\
**Post date:** [November 6, 2007, 2:11pm UTC](https://www.ruby-forum.com/t/string-to-symbol/119367/3 "2007-11-06T14:11:41Z")

</div>

Fernando C. wrote:

> puts :repetitive\_message  
> puts :repetitive\_message  
> puts :repetitive\_message

If you use

puts :“blah blah blah”

then no matter how many times you write it, the symbol literal will  
reference the same symbol (as long as the literal is the same, of  
course). Also, you can assign either a simple string, or a symbol to a  
variable, thus reusing it without repeating in the source.

my\_str = “some string”  
my\_sym = :“some symbol”

puts my\_str  
puts my\_sym

mortee

---

<div class="post-metadata">

**Author:** ![fernan2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fernan2](https://www.ruby-forum.com/u/fernan2)\
**Post date:** [November 6, 2007, 2:34pm UTC](https://www.ruby-forum.com/t/string-to-symbol/119367/4 "2007-11-06T14:34:29Z")

</div>

Fernando C. wrote:

> mortee wrote:
> 
> > my\_str = “some string”  
> > my\_sym = :“some symbol”
> 
> Right.  
> If I name the “variable” as “MY\_STR” is then officially a “constant”  
> (instead of a variable) right?  
> (and that would be preferred in this case)

If you wish. Note that you can’t create constants from within methods.

mortee

---

<div class="post-metadata">

**Author:** ![fernan2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fernan2](https://www.ruby-forum.com/u/fernan2)\
**Post date:** [November 6, 2007, 2:24pm UTC](https://www.ruby-forum.com/t/string-to-symbol/119367/5 "2007-11-06T14:24:32Z")

</div>

mortee wrote:

> > puts :repetitive\_message  
> > puts :repetitive\_message  
> > puts :repetitive\_message  
> > puts :repetitive\_message
> 
> If you use
> 
> puts :“blah blah blah”

Ha… I missed that you could prefix a string with “:” like that.

> then no matter how many times you write it, the symbol literal will  
> reference the same symbol (as long as the literal is the same, of  
> course). Also, you can assign either a simple string, or a symbol to a  
> variable, thus reusing it without repeating in the source.
> 
> my\_str = “some string”  
> my\_sym = :“some symbol”

Right.  
If I name the “variable” as “MY\_STR” is then officially a “constant”  
(instead of a variable) right?  
(and that would be preferred in this case)

---

<div class="post-metadata">

**Author:** ![fernan2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fernan2](https://www.ruby-forum.com/u/fernan2)\
**Post date:** [November 6, 2007, 3:01pm UTC](https://www.ruby-forum.com/t/string-to-symbol/119367/6 "2007-11-06T15:01:22Z")

</div>

mortee wrote:

> If you wish. Note that you can’t create constants from within methods.

Oh… why is that?

---

<div class="post-metadata">

**Author:** ![fernan2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fernan2](https://www.ruby-forum.com/u/fernan2)\
**Post date:** [November 6, 2007, 3:33pm UTC](https://www.ruby-forum.com/t/string-to-symbol/119367/7 "2007-11-06T15:33:34Z")

</div>

Fernando C. wrote:

> mortee wrote:
> 
> > If you wish. Note that you can’t create constants from within methods.
> 
> Oh… why is that?

I don’t know the exact reason. I guess that constants are intended to be  
assigned during the initialization phase of an application, while  
modules and classes are defined. Methods are primarily used during the  
actual normal operation of the program.

If you absolutely need to assign a constant from a method, e.g. because  
you define a method for doing some meta-programming magic, and it needs  
to create constants too, you can use Module#const\_set.

mortee

---

<div class="post-metadata">

**Author:** ![fernan2](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/c5a1d2/32.png) [@fernan2](https://www.ruby-forum.com/u/fernan2)\
**Post date:** [November 6, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/string-to-symbol/119367/8 "2007-11-06T16:31:32Z")

</div>

> If you wish. Note that you can’t create constants from within methods.

I think what you mean is the dynamic constant assignment like so  
def hi  
ABC = “hi”  
end

But you can generate them programmatically like so:

def test(arg)  
\_ = Object.const\_set(arg.capitalize, "this is the constant  
"+arg.capitalize)  
end

test ‘foo’  
test ‘bar’

puts Foo # “this is the constant Foo”  
puts Bar # “this is the constant Bar”

I love constants btw I even abuse them and dont care that I do! 🙂
