Ruby Study Notes (2)-basic usage of Classes

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.

 

Last testCodeShows the basic usage of classes in Ruby:

Class People # similar to dynamic languages such as JavaScript, classes in ruby are not public, and access control identifiers such as private def initialize (_ name) # constructors with fixed names: initialize @ name = _ name; # Convention: the private variable of the class starts with "@" enddef 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 line of the method in Ruby. It will be treated as a function value and return enddef get_name_sizereturn @ name. length # This method returns the number type (that is, the name length) enddef test # the last part of the class definition. This method is marked as a private method with puts "Private method (test) in people. "enddef show_nametest # private method, which can be called internally Puts "name = # {@ name}" endattr_accessor: name # defines a read/write attribute, which also reflects an important idea of Ruby: conventions over norms, because @ name has been used before, the attribute here only needs to remove @, Ruby will automatically and intelligently generate a property similar to set {value = @ name} in C }, the get {return @ name} statement private: Test # indicates that the test method is the protected of the private attribute: get_name_size # indicate 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) endapeople = people. new ("Jimmy"); # create a people instance puts apeople. to_string # Call the to_string method # Puts apeople. get_name_size # An error will be reported because this method is protected # Put S apeople. test # will report an error because this method is a private method apeople. show_nameputs apeople. nameapeople. name = "Yang junming" # modify the name apeople. show_name # define another subclass class man <peopledef initialize (_ name) Super @ sex = trueendattr_reader: sex # define the read-only attribute sexdef call_protected_methodputs get_name_size # Call the parent class's protected method enddef call_protected_method2 (Man1) puts man1.get _ name_size # note here: the parent class can be protected here, dynamically add to the subclass instance enddef call_private_method # The subclass can call the private method of the parent class !!! At the beginning, I was not used to testend def call_private_method2 (Man1) man1.test # Note: although the syntax check can be passed, a message indicating that the private method cannot be called is displayed during running, this is also the difference between private and protected endendputs "******************************" aman = man. new ("jimmy. yang "); Aman. show_nameaman.call_protected_methodputs Aman. sexaman. call_private_methodaman2 = 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 a type concept for variables, this is not the case. That is, when you call a protected method of the parent class, it is actually the puts aman to be matched by the type. class # display the Aman Class Name

The running result 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

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.