Convert string to symbol-able in ruby

后端 未结 7 1981
天涯浪人
天涯浪人 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:41

    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!

提交回复
热议问题