Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

symbols

ruby

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!

like image 375
cjm2671 Avatar asked Jan 18 '11 14:01

cjm2671


People also ask

What does before a variable mean in Ruby?

The @ symbol before a variable tells Ruby that we are working with an instance variable, and @@ before a variable tells us we are working with a class variable. We use @ before a variable in instance methods within a class to tell Ruby to access that attribute (instance variable) of the instance.

What does :: mean in rails?

:: Lets you access a constant, module, or class defined inside another class or module.

What does '@' mean in Ruby?

In Ruby, the at-sign ( @ ) before a variable name (e.g. @variable_name ) is used to create a class instance variable.

What is @variable in Ruby on Rails?

Ruby variables are locations which hold data to be used in the programs. Each variable has a different name. These variable names are based on some naming conventions. Unlike other programming languages, there is no need to declare a variable in Ruby.


1 Answers

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 :.

like image 139
Andy Lindeman Avatar answered Oct 09 '22 15:10

Andy Lindeman