Convertir string a symbol-able en ruby


Los símbolos generalmente se representan como tales

:book_author_title

Pero si tengo una cadena:

"Book Author Title"

¿Hay una forma incorporada en rails/ruby para convertirlo en un símbolo donde pueda usar la notación : sin hacer un reemplazo de expresiones regulares de cadena sin procesar?

Author: P Shved, 2010-01-05

6 answers

Rails tiene el módulo ActiveSupport::CoreExtensions::String::Inflections que proporciona dichos métodos. Vale la pena mirarlos. Para su ejemplo:

'Book Author Title'.parameterize.underscore.to_sym # :book_author_title
 319
Author: Priit,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-04 10:16:32

De: http://ruby-doc.org/core/classes/String.html#M000809

str.intern => symbol
str.to_sym => symbol

Devuelve el Símbolo correspondiente a str, creando el símbolo si no existía previamente. Véase Symbol#id2name.

"Koala".intern         #=> :Koala
s = 'cat'.to_sym       #=> :cat
s == :cat              #=> true
s = '@cat'.to_sym      #=> :@cat
s == :@cat             #=> true

Esto también se puede usar para crear símbolos que no se pueden representar usando la notación :xxx.

'cat and dog'.to_sym   #=> :"cat and dog"

Pero para su ejemplo ...

"Book Author Title".gsub(/\s+/, "_").downcase.to_sym

Debería ir;)

 209
Author: zzeroo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-01-05 16:00:46
"Book Author Title".parameterize('_').to_sym
=> :book_author_title

Http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-parameterize

Parametrize es un método rails, y le permite elegir lo que desea que sea el separador. Es un guión " - " por defecto.

 21
Author: Chris Ciollaro,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-15 14:44:02

Pasante → símbolo Devuelve el Símbolo correspondiente a str, creando el símbolo si no existía previamente

"edition".intern # :edition

Http://ruby-doc.org/core-2.1.0/String.html#method-i-intern

 13
Author: Julio Marins,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-24 15:24:21

En Rails puedes hacer esto usando el método underscore:

"Book Author Title".delete(' ').underscore.to_sym
=> :book_author_title

El código más simple está usando regex (funciona con Ruby):

"Book Author Title".downcase.gsub(/\s+/, "_").to_sym
=> :book_author_title
 10
Author: Chandra Patni,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-01-05 06:44:48

¿Es esto lo que estás buscando?:

:"Book Author Title"

:)

 9
Author: Kai Stinchcombe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-10 00:25:09