Joh*_*ase 29 ruby syntax hash hashrocket
Ruby中冒号:和胖箭的区别是什么=>?还是什么时候用?
:foo => true
foo: true
Run Code Online (Sandbox Code Playgroud)
JDu*_*til 26
语法用于定义Hash键/值对,差异取决于Ruby版本.
在Ruby 1.8和Ruby 1.9中都受支持
:foo => true
Run Code Online (Sandbox Code Playgroud)
仅在Ruby 1.9中受支持
foo: true
Run Code Online (Sandbox Code Playgroud)
如果你在Ruby 1.9中开发,你应该使用以下语法:
foo: true
Run Code Online (Sandbox Code Playgroud)
因为它似乎是社区的方向.
后者是1.9中引入的新Hash语法.例如,见:
http://breakthebit.org/post/8453341914/ruby-1-9-and-the-new-hash-syntax
两条线产生的散列是相同的.