the last hash in param in a function call can omit {}_Ruby_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > Ruby > the last hash in param in a function call can omit {}

the last hash in param in a function call can omit {}

 2011/9/5 8:11:16  peryt  http://peryt.iteye.com  我要评论(0)
  • 摘要:thereisonenoteabouttheelementorderinhashes:Ruby1.9actuallyguaranteesthathasheskeeptheirelementsinthesameorderentered,butitwouldbeunwiseevertocountonaparticularordering.==================================================let'sseethismethod
  • 标签:has can function Hash all

there is one note about the element order in hashes:


Ruby 1.9 actually guarantees that hashes keep their elements in the same order entered, but it would be unwise ever to count on a particular ordering.?

?

==================================================

?

let's see this method:

?

stylesheet_link_tag 'blueprint/screen', :media => 'screen'

?

you will find the last param is a hash, but why there is no {} around this hash?

?

the reason is this:

?

when the hash is the last argument in a function call, the {} is optional, the following two lines are equivalent:

?

?

stylesheet_link_tag 'blueprint/screen', { :media => 'screen' }
stylesheet_link_tag 'blueprint/screen', :media => 'screen'

?

note this only apply to a function call, not a function definition.

?

by the way, the html generated by the above code is:

?


<link href="/stylesheets/blueprint/screen.css" media="screen" rel="stylesheet"
type="text/css" />
<link href="/stylesheets/blueprint/print.css" media="print" rel="stylesheet"
type="text/css" />
发表评论
用户名: 匿名