Convert string to symbol-able in ruby

后端 未结 7 1919
天涯浪人
天涯浪人 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:44
    "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.

    0 讨论(0)
提交回复
热议问题