开发者

What does the : mean in rails before a variable name?

开发者 https://www.devze.com 2023-02-05 12:17 出处:网络
For example, : symbol - I\'m trying to work out what the : means, and how it differs, say, for example, from @ and also from no symbol whatsoever.

For example, : symbol - I'm trying to work out what the : means, and how it differs, say, for example, from @ and also from no symbol whatsoever.

If there's a guide that would be 开发者_如何学运维really helpful!


It's a symbol, which is a Ruby language construct.

Symbols are similar to strings, but this blog post explains the details.

@ means an instance variable on the class: it's basically a variable that's shared among all methods on an instance of a class. It has no relation to :.


: denotes that you are using a symbol.

@ is an instance variable - basically a variable that is set once and can be used through a ruby process until it is done.

Plain old variable_name is a local variable and that variable is only good for as long as that method is called.

As for guides, you should read up on ruby basics or take a look at the something like this


@ is an ivar : is an symbol (think it like a COSTANT in C, but without any numerical value)


http://rubylearning.com/satishtalim/ruby_symbols.html and http://www.troubleshooters.com/codecorn/ruby/symbols.htm are 2 well written guides (second one might be a bit old, but most of it applies in Ruby 1.9 too).

You might also want to check the official documentation on Symbol at http://www.ruby-doc.org/core/classes/Symbol.html

0

精彩评论

暂无评论...
验证码 换一张
取 消