Ruby-on-rails – Convert string to symbol-able in ruby

rubyruby-on-rails

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?

Best Answer

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