Extension and inheritance of classes
Class Fixnum
Def dosome(str)
Puts str
End
Def abs
Puts "overwrites the original method"
End
End
Puts 1.class
1.dosome("dynamically added method")
1.abs# overwrite the original method
Freeze Frozen Objects
Class Test
Attr_accessor :value
End
t=Test.new
T.value=1
a="test"
A.freeze
#a<<"1" Exception a<<"1" or cause a new object to be generated, a=a+"1" or point to the original object
a=a+"1"
Puts a
#t.freeze Freeze cannot be modified, so the following extension will modify the object and will report Error TypeError
Def t.newmethod()
Puts "new method of t"
End
Dup=t.dup
Clone=t.clone
T.value=2
Puts t.value
T.newmethod
Clone.newmethod#clone gets the method newmethod defined for t
#dup.newmethod dup does not get the method newmethod defined for t
=begin
Both dup and clone can copy the object's content data. The only difference is that the clone method can also retain the methods associated with the object.
Both dup and clone are shallow copies. If the object also contains references to other objects, the included objects will not be copied, but only their references will be copied.
=end
Serialization of
Class My_class
Attr_accessor :value
End
a=My_class.new
A.value=123
#Get a serialized character of a
Tempa=Marshal.dump(a)
Puts tempa
#Rebuild objects by serializing characters
b=Marshal.load(tempa)
Puts b.class
Puts b.value
=begin
Object serialization is not limited to marshal, there are other ways, marshal is a built-in module of Ruby, the performance is still relatively outstanding
=end
Module
Module MyModule
Def function
Puts "module object method"
End
Def self.function
Puts "module method"
End
End
Module MyModule
Def self.otherfunction
Puts "module method 2"
End
CONST1="Constant Constants"
Module MysubModule
CONST1="constant of submodule"
End
End
#p =MyModule.new
#p.function Module is not a concept of instance objects, using new will throw an exception
Puts MyModule.class
MyModule.function
MyModule.otherfunction
#:: To reference a module or method in a module, you can see a clear hierarchical relationship, and constants or methods with the same name in different modules will not cause conflicts.
MyModule::function
Puts MyModule::MysubModule::CONST1
Load Module (require,load,include,extend)
#load(‘testxml.rb‘, wrap) #Load testxml.rb file
#require ‘testxml‘ #Load the testxml library, usually also load the testxml.rb file
=begin
Both load and require load the corresponding module file into the current environment, and load will load the source file unconditionally, regardless of whether it has been loaded before.
Require will check to ensure that the module has only been loaded once, often used to load some extension libraries.
After require and load are loaded, local variables in the loaded file are not loaded.
If the incoming file does not contain a path, it will look for the search under the currently loaded directory.
=end
Module MyModule
Def method
Puts "method"
End
End
Class MyClass
Include MyModule
# because include function is mixed into the module, its role is equivalent to
# def method
# puts "method"
# end
End
Class MyClass1
Extend MyModule
The object extended by the #exted method depends on the object that calls the extend method. It is important to note that the extension is essentially equivalent to self.extend MyModule.
#self The code in self refers to the MyClass1 class, so MyModule is defined as the class method of MyClass1.
# def self.method
# puts "method"
# end
End
My_class=MyClass.new
The #class uses the method in the include module to become an instance method of the class
My_class.method
The method in the #class uses the method in the extend module to become the class method.
MyClass1.method
Aa="0"
#All objects can extend the module and live in the module method, because the aa variable refers to an instance of a string object, and the object called by extend is an instance object.
#的extend is also an instance object
Aa.extend(MyModule)
Aa.method
=begin
Include and extend do not automatically load files, they simply mix modules into existing modules or classes.
Include will create a reference from the class to the contained module, automatically added as an instance method of the class, when the module changes, the distribution in the class will also change accordingly.
Extend and inc are very similar, but extend is used to introduce a module into an object, so that this object also has a method of this module.
=end
Ruby class, Module 1