A real variable starts at the @, its scope is limited to the self object. Two different objects, even if they belong to the same class, can have real variables of different values. From outside the object, the real variable cannot be changed or even observed (for example, Ruby's real variable is never public), Unless the method is explicitly stated by the programmer. Like global variables, the value of a real variable before its inception is nil.
Ruby's real variables need not be declared. This implies the elastic structure of the object. In fact, each real variable is dynamically added to the object at the first occurrence.
Ruby> class Insttest
| def set_foo (N)
| @foo = n
| End
| def set_bar (N)
| @bar = n
| End
| End
Nil
ruby> i = insttest.new
#<insttest:0x83678>
Ruby> I.set_foo (2)
2
Ruby> I
#<insttest:0x83678 @foo =2>
Ruby> I.set_bar (4)
4
Ruby> I
#<insttest:0x83678 @foo =2, @bar =4>
Note that the value of @bar is reported in the previous example until the Set_bar method I was called.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service