Basic usage of ruby learning notes (2)

Source: Internet
Author: User

Some important differences between ruby and c # are as follows:
1. ruby is a dynamic language, and c # Is a static language-that is, after an object is new, ruby can dynamically add some attributes or methods to the object instance (as does javascript)
2. ruby deliberately weakens the concept of variable types. By default, no specific (return) types need to be declared for variables/methods, but in ruby, the type is automatically assigned based on the value of the variable. (You can view it through "puts variable. class)
3. Compared with c #, ruby may be a bit of a Ray: The private member in the parent class can be used in the subclass!
... Other places, and write it out after learning.
The last test code shows the basic usage of classes in ruby:
Copy codeThe Code is as follows:
Class People # similar to dynamic languages such as javascript, classes in ruby do not have public or private access control identifiers.

Def initialize (_ name) # constructor with a fixed name: initialize
@ Name = _ name; # Convention: the private variable of the class starts @
End

Def to_string # similar to the practice in C #. Here we will write a ruby version of The ToString method.
"My name is # {@ name}" # the value of the last row of the method in ruby is returned as a function value.
End

Def get_name_size
Return @ name. length # This method returns a number (that is, the name length)
End

Def test # mark this method as a private method in the last part of the class definition.
Puts "private method (test) in People ."
End

Def show_name
Test # private method, which can be called internally
Puts "name =#{@ name }"
End

Attr_accessor: name # defines a read/write attribute, which also reflects an important idea of ruby: conventions are more important than norms, because @ name has been used before, the attribute here only needs to remove @. ruby will automatically and intelligently generate statements similar to set {value = @ name} And get {return @ name} in c #.

Private: test # indicates that the test method is private.
Protected: get_name_size # It indicates that get_name_size can only be used in this class or subclass definition (or assign a value to the subclass instance in the subclass definition)

End
APeople = People. new ("jimmy"); # create an instance of People
Puts aPeople. to_string # Call the to_string Method
# Puts aPeople. get_name_size # An error is reported because this method is protected.
# Puts aPeople. test # An error is reported because the method is a private method.
APeople. show_name
Puts aPeople. name
APeople. name = "Yang junming" # Change the name
APeople. show_name
# Define a subclass
Class Man <People
Def initialize (_ name)
Super
@ Sex = true
End

Attr_reader: sex # defines the read-only attribute sex

Def call_protected_method
Puts get_name_size # Call the protected method of the parent class
End

Def call_protected_method2 (man1)
Puts man1.get _ name_size # Note: The protected method of the parent class can be dynamically added to the subclass instance.
End

Def call_private_method # The subclass can call the private method of the parent class !!! This is not a habit at the beginning.
Test
End

Def call_private_method2 (man1)
Man1.test # Note: although the syntax check is successful, a message indicating that the private method cannot be called is displayed at runtime. This is also the difference between private and protected.
End


End
Puts "******************************"
AMan = Man. new ("jimmy. yang ");
AMan. show_name
AMan. call_protected_method
Puts aMan. sex
AMan. call_private_method
AMan2 = Man. new ("Mike ")
AMan. call_protected_method2 (aMan2 );
# AMan. call_private_method2 (aMan2 );
A = "abc ";
# AMan. call_protected_method2 (a); # Although ruby does not have the type concept for the variables, this is not acceptable. That is, when calling the protected method of the parent class, it actually requires type matching.
Puts aMan. class # display the aMan class Name

The running result is as follows:
Copy codeThe Code is as follows:
> Ruby classDemo. rb
My name is jimmy
Private method (test) in People.
Name = jimmy
Jimmy
Private method (test) in People.
Name = Yang junming
******************************
Private method (test) in People.
Name = jimmy. yang
10
True
Private method (test) in People.
4
Man
> Exit code: 0

Author: Yang Guo under the bodhi tree
Source: http://yjmyzz.cnblogs.com

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.