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 test Code Shows the basic usage of classes in Ruby:
Copy code The 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