# 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;