2012-10-25 42 views
3

可能重複:
Understanding Symbols In Ruby
What is the colon operator in Ruby?意義結腸:在Ruby中

我真的覺得幼稚問這個,但我會繼續前進,問:

:在紅寶石中的重要性是什麼?

我已經看到它在很多地方使用,如params[:id]或類似x < :length

+0

重複:http://stackoverflow.com/questions/2341837/understanding-symbols-in-ruby和http://stackoverflow.com/questions/6337897/what-is-the-colon-operator- in-ruby?rq = 1 –

回答

6

冒號表示「符號」。符號就像一個字符串,但它是不可變的(你不能改變它的內容)。在幕後,它也佔用較少的內存,因爲一個符號只需要在內存中存在一次(即稱爲「length」的兩個字符串在內存中將存在兩次,但稱爲「length」的兩個符號將指向同一對象)。

+1

我會說一個':'後跟一個單詞_denotes_的符號,而'''它自己會引發語法錯誤。 – Candide

2

它是類型解釋的語法指示。

0-9 numeric* 
:  symbol 
"" string 
[] array 
{} hash 
  • 模式在現實中更復雜。