Explain please, I can not understand.
class Foo
@a = 123
@@b = 123
end
What are the advantages of variable objects-classes and the class variables? When should I use first, and in which the second?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Instance level variables area created anew for each instance of the class. For example, a variable
@idshould probably be unique for each instance ofFoo. However, there may be some values that should be the same for every instance of the type. In that case, a class variable would be more appropriate.One important side effect of class level variables is that they are shared amongst derived classes as well. This means that changing the value in a subclass of ‘Foo’ will change it for ‘Foo’ objects as well. This may be what you want, but it can be a bit surprising to find out the hard way.
For example: