Module full solution ======>> the ruby class is a single inheritance creature, so the module appears, implementing multi-Inheritance

Source: Internet
Author: User

The ruby class is a single inheritance creature, so there is a module, implementing multi-inheritance,

Note: The names of modules, constants, and classes start with an uppercase letter.

, Module reference:
1. If the referenced module is in the same file, use the module operation directly.
2. if not in the same file, use require to load the file where the referenced module is located. (The full name is referenced)

You can use module to include various classes, and then create a new class. Use include to include various classes to implement multi-inheritance and =. = It's really a complex creature, and it's easy to think that it's more complicated,

In fact, modeule cannot be instantiated, so we can only reference the method through module. method, of course, first come to a sentence

instance_method :fun1 


Ruby provides public, protected, and private to modify class members, which have the same meaning as C ++. But the difference is that in Ruby, all class variables are private by default, and class functions are public by default. For Class Members, you can also use attr_accessor to modify them as in row 4th so that they can be accessed externally.

Class includes multiple modules

 1 module Mod1  
2 def fun1
3 puts "Mod1::fun1"
4 end
5 instance_method :fun1
6 end
7 module Mod2
8 def fun2
9 puts "Mod2::fun2"
10 end
11 instance_method :fun2
12 end
13 class Cls
14 include Mod1
15 include Mod2
16 end
17 obj = Cls.new
18 obj.fun1
19 obj.fun2

Module inclusion class

 1 module Mod  
2 def myFunction
3 puts "myFunction"
4 end
5 module_function :myFunction
6 class Class
7 def yourFunction
8 puts "Mod::Class.yourFunction"
9 end
10 end
11 end
12 Mod.myFunction
13 object = Mod::Class.new
14 object.yourFunction

Assume that the module defines the following methods:

 1 module MyModule 
2   GOODMOOD = "happy"
3   BADMOOD = "grumpy"
4 def greet
5 return "I'm #{GOODMOOD}. How are you?"
6 end
7 def MyModule.greet
8 return "I'm #{BADMOOD}. How are you?"
9 end
10 end

The MyModule. greet method is equivalent

1   def self.greet
2 return "I'm #{BADMOOD}. How are you?"
3 end

Constants defined here can be accessed using the following methods:

MyModule::GOODMOOD

When you want to reference the greet method one day, you can use

MyModule.greet

However, if you find it cumbersome, You can include this module first.

include MyModule 
puts( greet )

Note that using the include method only references the greet method, rather than the MyModule. greet, the module's own method can only be extracted by the module name, And the include method cannot be used to reduce the typing pain,

Take the above example, write a class, and reference the above module. That's right! This is the implementation method of RUBY multi-inheritance!

class MyClass  include MyModule  def sayHi puts    (greet)  endend

In the class, include contains the above modules. Okay, let's instantiate them.

1 ob = MyClass.new 
2 ob.sayHi
3 puts(ob.greet)

You can be surprised to find that the Instance Object of the class can also access the instance method of the module. This is amazing. It is equivalent to inheriting all the methods of the module.

The following is an example of multi-inheritance.

 1 module MagicThing
2 attr_accessor :power
3 end
4
5 module Treasure
6 attr_accessor :value
7 attr_accessor :owner
8 end
9
10 class Weapon
11 attr_accessor :deadliness
12 end
13
14 class Sword < Weapon
15 include Treasure
16 include MagicThing
17 attr_accessor :name
18 end

From the above, we have set up multiple modules, MagicThing and Treasure, and a Weapon class, all of which are implemented in Sword inheritance ~ First, Sword inherits Weapon and then introduces two modules with include. Therefore, the following implementation method is completely true!

1 s = Sword.new
2 s.name = "Excalibur"
3 s.deadliness = "fatal"
4 s.value = 1000
5 s.owner = "Gribbit The Dragon"
6 s.power = "Glows when Orcs Appear"

Next, let's take a look at the variables in the module. Note that the local variables in the module cannot be called outside the module, or even the return is not. This is why Internal modules why is the instance variable flying?

 1 x = 1 # local to this program
2
3 module Foo
4 x = 50 # local to module Foo
5
6 # This can be mixed in but the variable x won't then be visible
7 def no_bar
8 return x
9 end
10
11
12 def bar
13 @x = 1000
14 return @x
15 end
16
17 puts("In Foo: x = #{x}") # this can access the „module local‟ x
18 end
19
20
21 include Foo
22
23 puts(x)
24 #puts(no_bar) # Error! This can't access the module-local variable
25 ## needed by the no_bar method
26 puts(bar)

We can see that we have blocked 24th lines of code, because this method is wrong! Cannot be called out,

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.