Convert string to symbol-able in ruby

后端 未结 7 1977
天涯浪人
天涯浪人 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.

提交回复
热议问题