I have code @array = array
. What does putting the @
sign before array
do?
Asked
Active
Viewed 115 times
0
-
it creates instance variable instead of local variable. there is a nice article about instance variables: http://rubymonk.com/learning/books/4-ruby-primer-ascent/chapters/45-more-classes/lessons/110-instance-variables – Iuri G. Feb 03 '14 at 16:50
-
It makes the variable an instance variable. Please read here: http://www.railstips.org/blog/archives/2006/11/18/class-and-instance-variables-in-ruby/ – lucke84 Feb 03 '14 at 16:50
-
2Please read chapter 1 in the Ruby programming book, before asking such elementary questions. – Niels B. Feb 03 '14 at 17:40
-
1@NielsB.I didnt know there was such a requirement. – Iuri G. Feb 03 '14 at 17:48
-
1I agree with @Niels. Explaining what `@` means is hand-holding in the extreme. – Cary Swoveland Feb 03 '14 at 18:18
-
1If you hover over the "downvote" link, you will see a short list of reasons for downvoting. Obviously, if those are reasons for downvoting, then the community thinks that those are not acceptable for questions. And one of them is "does not show any research effort". Asking something which is explained in the very first chapter of pretty much every Ruby book ever written, most certainly does qualify. – Jörg W Mittag Feb 03 '14 at 21:26
-
1@modulus, no, Jörg is not the police, but he is a well respected member of the Stack Overflow Ruby community, who understands how this site works. While you might not like having to show effort in your questions, you'll quickly find that doing so will pay off with positive votes, and, conversely, failing to do so will result in downvotes. – the Tin Man Feb 03 '14 at 22:17
1 Answers
3
Variables in the form @<something>
are instance variables in Ruby. They are part of the class in which you create them.
For example in:
class Something
def initialize(x)
@x = x
end
end
@x
is an instance variable of the class Something
while x
is a simple local variable of the method initialize
.

Shoe
- 74,840
- 36
- 166
- 272