Ruby attr_accessor, Ruby @ member variables and temporary variables

Source: Internet
Author: User
# Class describ ------ rdoc rules
# = Headers
# This is header content
#== Sub Header
# This is sub header content
#=== Sub Header
# This is sub Header
Class animal

Attr_accessor: name # ------------- ①

# This is the comment for the 'new' method!

Def initialize type
Name = type # ------------- ②
End

# This is comment for 'shout' Method
# Second line
# * This is sub line 1
# * This is sub Line 2

Def shout
If name = 'Dog'
Puts "Wang! Wang! "
Elsif name = 'cat'
Puts "Miao! Miao! "
Elsif name = Nil
Puts "nil"
Else
Puts "Kao! "
End
End

End

Dog = animal. New "dog"
Puts dog. Name
Dog. Shout
Dog. Name = "cat"
Dog. Shout

The output of this program is:

Nil

Miao! Miao!

-----------------

①: Member variables

②: Temporary variables

Attr_accessor indicates that the next member is a class member and provides external access capabilities for it. That is, it is equivalent to providing get; set;

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.