将字符串转换为 ruby 中的符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2004491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert string to symbol-able in ruby
提问by Christopher
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?
rails/ruby 中是否有内置方法将其转换为一个符号,我可以在其中使用:符号,而无需执行原始字符串正则表达式替换?
回答by Priit
Rails got ActiveSupport::CoreExtensions::String::Inflectionsmodule that provides such methods. They're all worth looking at. For your example:
Rails 获得ActiveSupport::CoreExtensions::String::Inflections了提供此类方法的模块。都值得一看。对于您的示例:
'Book Author Title'.parameterize.underscore.to_sym # :book_author_title
回答by zzeroo
from: http://ruby-doc.org/core/classes/String.html#M000809
来自:http: //ruby-doc.org/core/classes/String.html#M000809
str.intern => symbol
str.to_sym => symbol
Returns the Symbol corresponding to str, creating the symbol if it did not previously exist. See Symbol#id2name.
返回对应于str的符号,如果之前不存在则创建该符号。见Symbol#id2name。
"Koala".intern #=> :Koala
s = 'cat'.to_sym #=> :cat
s == :cat #=> true
s = '@cat'.to_sym #=> :@cat
s == :@cat #=> true
This can also be used to create symbols that cannot be represented using the :xxxnotation.
这也可用于创建无法使用:xxx符号表示的符号。
'cat and dog'.to_sym #=> :"cat and dog"
But for your example ...
但是对于你的例子......
"Book Author Title".gsub(/\s+/, "_").downcase.to_sym
should go ;)
应该去 ;)
回答by Chris Ciollaro
"Book Author Title".parameterize('_').to_sym
=> :book_author_title
http://api.rubyonrails.org/classes/ActiveSupport/Inflector.html#method-i-parameterize
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.
parameterize 是一个 rails 方法,它可以让你选择你想要的分隔符。默认情况下它是一个破折号“-”。
回答by Julio Marins
intern → symbol Returns the Symbol corresponding to str, creating the symbol if it did not previously exist
实习生 → 符号 返回与 str 对应的符号,如果之前不存在则创建该符号
"edition".intern # :edition
回答by Chandra Patni
In Rails you can do this using underscoremethod:
在 Rails 中,您可以使用以下underscore方法执行此操作:
"Book Author Title".delete(' ').underscore.to_sym
=> :book_author_title
The simpler code is using regex (works with Ruby):
更简单的代码使用正则表达式(适用于 Ruby):
"Book Author Title".downcase.gsub(/\s+/, "_").to_sym
=> :book_author_title
回答by Kai Stinchcombe
Is this what you're looking for?:
这是您要找的吗?:
:"Book Author Title"
:)
:)
回答by Fábio Araújo
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!
希望它可以帮助像我这样的人!

