3

Possible Duplicate:
Understanding Symbols In Ruby
What is the colon operator in Ruby?

I really feel naive asking this, but I'm going to go ahead and ask :

What is the importance of : in ruby ?

I have seen it being used in a number of places like params[:id] or like x < :length.

Community
  • 1
  • 1
OneMoreError
  • 7,518
  • 20
  • 73
  • 112
  • Duplicate of: http://stackoverflow.com/questions/2341837/understanding-symbols-in-ruby and http://stackoverflow.com/questions/6337897/what-is-the-colon-operator-in-ruby?rq=1 – the Tin Man Oct 26 '12 at 06:12

3 Answers3

6

A colon denotes a "symbol". A symbol is like a string, but it is immutable (you can't change its contents). Behind the scenes, it also takes up less memory, since a symbol only needs to exist once in memory (i.e., two strings called "length" will exist twice in memory, but two symbols called :length will point to the same object).

mipadi
  • 398,885
  • 90
  • 523
  • 479
  • 1
    I'd say a `:` followed by a word _denotes_ a symbol, whereas a `:` on its own will raise a syntax error. – Candide Oct 25 '12 at 14:17
2

:length means it is a Symbol

Symbols are Strings, just with an important difference, Symbols are immutable.

RubyDoc: Symbol objects represent names and some strings inside the Ruby interpreter. They are generated using the :name and :"string" literals syntax, and by the various to_sym methods.

Here are some good places to learn more about symbols

rogeliog
  • 3,632
  • 4
  • 27
  • 26
2

It is syntax indication of type for interpreter.

0-9   numeric*
:     symbol
""    string
[]    array
{}    hash
  • Patterns more complicated in reality.
SwiftMango
  • 15,092
  • 13
  • 71
  • 136