Ruby Syntax: attr_reader, attr_writer, and attr_accessor

Source: Internet
Author: User
Attr_reader: abc

You can use

Instance name. abc obtains the value of this variable, but cannot be modified. It is equivalent

Def abc
Return @ abc
End

By the way, attr_writer and attr_accessor,
Attr_writer: abc, which is equivalent

Def abc = (abc)
@ Abc = abc
End

In this case, you cannot obtain the value of abc through Instance name. abc, but you can
Instance name. abc = expression set abc Value

Attr_accessor: abc

Equivalent

Def abc
Return @ abc
End
Def abc = (abc)
@ Abc = abc
End

You can use the Instance name. abc or the Instance name. abc = value.

Sometimes, obtaining/setting is not a simple internal variable assignment. Therefore, you must use attr_reader and attr_writer to partially simplify the definition description.
See Game_Battler's
Attr_reader: hp
And
Def hp = (hp)
Two methods

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.