Convert string to symbol-able in ruby

后端 未结 7 1953
天涯浪人
天涯浪人 2020-12-22 15:41

Symbols are usually represented as such

:book_author_title

but if I have a string:

\"Book Author Title\"

7条回答
  •  [愿得一人]
    2020-12-22 16:37

    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
    

提交回复
热议问题