Convert string to symbol-able in ruby

Ruby on-RailsRuby

Ruby on-Rails Problem Overview


Symbols are usually represented as such

:book_author_title

but if I have a string:

"Book Author Title"

is there a built in way in rails/ruby to convert it into a symbol where I can use the : notation without just doing a raw string regex replace?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Rails got ActiveSupport::CoreExtensions::String::Inflections module that provides such methods. They're all worth looking at. For your example:

'Book Author Title'.parameterize.underscore.to_sym # :book_author_title

Solution 2 - Ruby on-Rails

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

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

Returns the Symbol corresponding to str, creating the symbol if it did not previously exist. See Symbol#id2name.

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

This can also be used to create symbols that cannot be represented using the :xxx notation.

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

But for your example ...

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

should go ;)

Solution 3 - Ruby on-Rails

"Book Author Title".parameterize('_').to_sym
=> :book_author_title

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

parameterize is a rails method, and it lets you choose what you want the separator to be. It is a dash "-" by default.

Solution 4 - Ruby on-Rails

intern → symbol Returns the Symbol corresponding to str, creating the symbol if it did not previously exist

"edition".intern # :edition

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

Solution 5 - Ruby on-Rails

Is this what you're looking for?:

:"Book Author Title"

:)

Solution 6 - Ruby on-Rails

In Rails you can do this using underscore method:

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

The simpler code is using regex (works with Ruby):

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

Solution 7 - Ruby on-Rails

This is not answering the question itself, but I found this question searching for the solution to convert a string to symbol and use it on a hash.

hsh = Hash.new
str_to_symbol = "Book Author Title".downcase.gsub(/\s+/, "_").to_sym
hsh[str_to_symbol] = 10
p hsh
# => {book_author_title: 10}

Hope it helps someone like me!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChristopherView Question on Stackoverflow
Solution 1 - Ruby on-RailsPriitView Answer on Stackoverflow
Solution 2 - Ruby on-RailszzerooView Answer on Stackoverflow
Solution 3 - Ruby on-RailscciollaroView Answer on Stackoverflow
Solution 4 - Ruby on-RailsJulio MarinsView Answer on Stackoverflow
Solution 5 - Ruby on-RailsKai StinchcombeView Answer on Stackoverflow
Solution 6 - Ruby on-RailsChandra PatniView Answer on Stackoverflow
Solution 7 - Ruby on-RailsFábio AraújoView Answer on Stackoverflow