Ruby ruby symbol 与 string 的区别

xautjzd · August 18, 2013 · Last by hiveer replied at September 28, 2016 · 4376 hits

今天浏览博客,无意中发现了一篇讲关于 symbol 与 string 的区别的博文,觉得分析的非常到位,便在此共享,供大家学习。这是大部分 rubyist 的疑问,特别是初入 ruby 的 programmer,看过这篇文章,估计几乎就能弄明白。 文章链接

You can't call any of the String methods like #upcase, #split on Symbols 在 irb 试了一下,居然可以调用 `:simlegate.upcase #=> :SIMLEGATE

#1 楼 @simlegate 确实可以,这篇文章比较早,估计针对当时的版本来说的,可能后来 rails 有所改观,但是其他大致都还讲的挺明白

我觉得能用 symbol 就用 symbol,这会带来更好的性能

#3 楼 @imlcl symbol 现在确实用的比较多

#3 楼 @imlcl 但是 symbol 不会被 GC。如果你是动态的要创建很多 symbol 的话,这种场景应该慎用。

You need to Sign in before reply, if you don't have an account, please Sign up first.