string to symbol

  • Thread starter Fernando Cacciola
  • Start date
F

Fernando Cacciola

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
 
M

mortee

Fernando said:
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

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
 
F

Fernando Cacciola

mortee said:
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)
 
M

mortee

Fernando said:
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
 
M

mortee

Fernando said:
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
 
M

Marc Heiler

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

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,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top