tags:

views:

152

answers:

4

Hi,

I'm new to the Ruby and Ruby on Rails world. I've read some guides, but i've some trouble with the following syntax. I think that the usage of :condition syntax is used in Ruby to define a class attribute with some kind of accessor, like:

class Sample
  attr_accessor :condition
end

that implicitly declares the getter and setter for the "condition" property. While i was looking at some Rails sample code, i found the following examples that i don't fully understand.

For example:

@post = Post.find(params[:id])

Why it's accessing the id attribute with this syntax, instead of:

@post = Post.find(params[id])


Or, for example:

@posts = Post.find(:all) 

Is :all a constant here? If not, what does this code really means? If yes, why the following is not used:

@posts = Post.find(ALL)

Thanks

+4  A: 

A colon before text indicates a symbol in Ruby. A symbol is kind of like a constant, but it's almost as though a symbol receives a unique value (that you don't care about) as its constant value.

When used as a hash index, symbols are almost (but not exactly) the same as using strings.

Also, you can read "all" from :all by calling to_s on the symbol. If you had a constant variable ALL, there would be no way to determine that it meant "all" other than looking up its value. This is also why you can use symbols as arguments to meta-methods like attr_accessor, attr_reader, and the like.

You might want to read up on Ruby symbols.

Mark Rushakoff
So it seems to me that a symbol seems like an Enum value in Java or c#, right?
Marco
@Marco: No, symbols in Ruby are nothing like enum values. They are more like symbols in Lisp or symbols in Smalltalk.
Jörg W Mittag
Got it, thanks!
Marco
They are kind of like enums insofar as they're given by name in your source code, and evaluate to a constant at runtime, but they're not the same... e.g. enums are intered at compile-time and symbols are interned at run-time.
John Douthat
An article which describes symbols in terms of saving memory is horribly backward. Saving memory should be a side-effect of doing things right, not the purpose of using them!
Andrew Grimm
A: 

This has nothing to do with Rails, it's just Ruby's Symbols. :all is a symbol which is effectively just a basic string.

thenduks
A: 

:all is a symbol. Symbols are Ruby's version of interned strings. You can think of it like this: There is an invisible global table called symbols which has String keys and Fixnum values. Any string can be converted into a symbol by calling .to_sym, which looks for the string in the table. If the string is already in the table, it returns the the Fixnum, otherwise, it enters it into the table and returns the next Fixnum. Because of this, symbols are treated at run-time like Fixnums: comparison time is constant (in C parlance, comparisons of symbols can be done with == instead of strcmp)

You can verify this by looking at the object_id of objects; when two thing's object_ids are the same, they're both pointing at the same object.

You can see that you can convert two strings to symbols, and they'll both have the same object id:

"all".to_sym.object_id == "all".to_sym.object_id #=> true

"all".to_sym.object_id == :all.object_id #=> true

But the converse is not true: (each call to Symbol#to_s will produce a brand new string)

:all.to_s.object_id == :all.to_s.object_id #=> false

John Douthat
A: 

Don't look at symbols as a way of saving memory. Look at them as indicating that the string ought to be immutable. 13 Ways of Looking at a Ruby Symbol gives a variety of ways of looking at a symbol.

To use a metaphor: symbols are for multiple-choice tests, strings are for essay questions.

Andrew Grimm