【发布时间】:2015-04-19 08:52:26
【问题描述】:
我正在尝试将字符串 "country." 转换为符号。我希望收到:country.,包括.。
我尝试了以下方法,但它们不起作用,因为我的符号仍然有引号。
"country.".to_sym; #=> :"country."
"country.".intern; #=> :"country."
"country.".parameterize.underscore.to_sym; #=> :country
"country\.".to_sym; #=> :"country."
【问题讨论】:
-
它已转换为 :country。为你? (包括点作为符号的一部分)
-
是的,它正在工作。甚至您自己的代码也显示它有效,至少在第 1、2 和 4 行中,句点明显存在。
-
一切顺利...:
"country.".to_sym.class #=> Symbol