Ruby classes, objects, variables

Source: Internet
Author: User

7. Class
(1) Initialize method, Initialize is a special method, the default is private, when calling new to create a new object, Ruby first allocates some memory to hold the uninitialized object, and then calls the object's Initialize method,
(2) The inspect method formats the ID and instance variables of an object by default
(3) Ruby classes are never closed, meaning that methods can be added to an existing class, including the built-in classes
(4) Ruby classes allow inheritance, such as Class child < Parent
(5) Inheritance and mixins Ruby support single inheritance, but can be introduced from any number of mixin features, which provides a controllable, similar to the ability of multiple inheritance.
(6) Convenient and quick to define the access method of attributes, such as:
Class Song
Attr_reader:name,: Artist,:d uration
End
Similar methods are: Attr_writer Write properties
(7) The virtual attribute creates a virtual instance variable using the property creation method, which is the same as other properties, but does not have a corresponding instance variable internally, such as:
Class Song
def duration_in_minutes
@duration/60.0
End
def duration_in_minutes= (value)
@duration = (value*60). to_i
End
(8) A class variable is similar to a class static variable in C + +, starting with two @, such as: @ @count
(9) Class methods are similar to class static functions in C + +, and the way in which class methods are defined is also
Class Demo
def demo.meth1

...

End
def self.meth2

...

End
Class << Self
def Meth3

...

End
End
End

8. Singleton mode
Class MyLogger
Private_class_method:new
@ @logger = Nil
def mylogger.create
@ @logger = new unless @ @logger
@ @logger
End
End
Note: Private_class_method declares a class method as private;
9. A variable is not an object, but a reference to an object:
Person1 = "Tim";p erson2=person1
person1[0]= "J"
Person2
Assigning Person1 to Person2 does not create any new objects; it simply copies Person1 object references to Person2, so Person1 and Person2 point to the same object. An assignment alias object, potentially giving you multiple variables referencing the same object, can be avoided by using the string's DUP method.
Person2 = Person1.dup
10. You can freeze an object to prevent others from modifying it, such as Person1.freeze

Ruby classes, objects, variables

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.