-3

Possible Duplicate:
What is the colon operator in Ruby?

While learning Ruby I've come across the ":" operator on occasion. Usually I see it in the form of

:symbol => value

what does it mean?

Community
  • 1
  • 1
Reda
  • 1,277
  • 1
  • 13
  • 27

1 Answers1

-1

It just indicates a that it is a symbol instead of a string. In ruby, it is common to use symbols instead of strings.

{:foo => value}
{'foo' => value}

It's basically a short-hand way of expressing a string. It can not contain spaces as you can imagine so symbols usually use underscores.

Try this on your own:

foo = :bar
foo.to_s # means to string
baz = 'goo'
baz.to_sym # means to symbol
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
weexpectedTHIS
  • 3,358
  • 1
  • 25
  • 30
  • Actually, the bit about symbols not being able to contain spaces is false; a symbol can hold anything a string can hold. `:"hello world"` contains a space, and yet it's a perfectly valid symbol. The difference between a string and a symbol is that a symbol always takes the same spot in memory every time you reference the same symbol. This is good in that it saves memory if you use it the right way; it's bad because it consumes memory if you don't. See: [Symbol](http://www.ruby-doc.org/core-1.9.3/Symbol.html). – Jeremy Rodi Sep 01 '12 at 19:43
  • 2
    Boiling down symbols to "basically a shorthand way of expressing a string" is *very* wrong and misleading. Symbols are very much not Strings. – Andrew Marshall Sep 01 '12 at 19:52
  • @AndrewMarshall I don't think Symbols should ever be used as strings; that's one of the bad ways of using symbols, and is terrible, especially for web development. Sorry if it came off that way. – Jeremy Rodi Sep 01 '12 at 20:28
  • @drderp: I think Andrew Marshall was referring to the answer above, not to your comment. Your comment is correct. The answer is plain wrong. It's a pity it got accepted. – undur_gongor Sep 01 '12 at 20:44
  • @undur_gongor I should have posted my comment as another answer :/ – Jeremy Rodi Sep 01 '12 at 21:47
  • @drderp I was replying to the answer, your comment is okay as undur_gongor says. – Andrew Marshall Sep 02 '12 at 03:09