PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > string to symbol
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
string to symbol

Réponse
 
LinkBack Outils de la discussion
Vieux 06/11/2007, 12h06   #1
Fernando Cacciola
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut string to symbol

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


--
Fernando Cacciola
SciSoft
http://fcacciola.50webs.com


  Réponse avec citation
Vieux 06/11/2007, 12h50   #2
mortee
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to symbol

Fernando Cacciola wrote:
> 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


  Réponse avec citation
Vieux 06/11/2007, 13h14   #3
Fernando Cacciola
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to symbol

mortee wrote:
> Fernando Cacciola wrote:
>> 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"
>

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)


--
Fernando Cacciola
SciSoft
http://fcacciola.50webs.com



  Réponse avec citation
Vieux 06/11/2007, 13h32   #4
mortee
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to symbol

Fernando Cacciola 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


  Réponse avec citation
Vieux 06/11/2007, 13h45   #5
Fernando Cacciola
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to symbol

mortee wrote:
> Fernando Cacciola 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.
>

Oh... why is that?


--
Fernando Cacciola
SciSoft
http://fcacciola.50webs.com


  Réponse avec citation
Vieux 06/11/2007, 14h25   #6
mortee
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to symbol

Fernando Cacciola 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


  Réponse avec citation
Vieux 06/11/2007, 15h32   #7
Marc Heiler
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: string to symbol

> 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!
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h06.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11748 seconds with 15 queries