What's the difference between colon “:” and fat arrow “=>” [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-17 16:27:03

问题


What's the difference between colon : and fat arrow => in Ruby? Or when to use what?

:foo => true
foo: true

回答1:


The syntax is for defining Hash key/value pairs, and the difference depends on the Ruby version.

Supported in both Ruby 1.8 and Ruby 1.9

:foo => true

Supported only in Ruby 1.9

foo: true

If you're developing in Ruby 1.9 you should probably use the syntax:

foo: true

as it appears to be the direction the community is moving in.




回答2:


The latter is the new Hash syntax introduced in 1.9. See, for example:

http://breakthebit.org/post/8453341914/ruby-1-9-and-the-new-hash-syntax

The hashes that the two lines generate are identical.



来源:https://stackoverflow.com/questions/8198811/whats-the-difference-between-colon-and-fat-arrow

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