What does the Ruby method 'to_sym' do?

≡放荡痞女 提交于 2019-12-18 10:58:24

问题


What does the to_sym method do? What is it used for?


回答1:


to_sym converts a string to a symbol. For example, "a".to_sym becomes :a.

It's not specific to Rails; vanilla Ruby has it as well.

It looks like in some versions of Ruby, a symbol could be converted to and from a Fixnum as well. But irb from Ruby 1.9.2-p0, from ruby-lang.org, doesn't allow that unless you add your own to_sym method to Fixnum. I'm not sure whether Rails does that, but it doesn't seem very useful in any case.



来源:https://stackoverflow.com/questions/4062929/what-does-the-ruby-method-to-sym-do

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!