The module in ruby is a bit similar to the namespace in. net. It can be used to distinguish the module with the same name but belongs to different developers (or organizations ).Code.
The following code defines an me module, in which SQRT is the same as SQRT in the math module, and there is also a duplicate name constant pi
# Define a module (somewhat similar.. Net namespace) module medef sqrt (num1, num2 =-1) Return "num1 =#{ num1}, num2 =#{ num2}" endpi = 3.14; endputs Math :: pi # The constant puts math can only be referenced through full reference before any modules are included. SQRT (2) # reference method point, reference constants use double colon puts "*" * 50 # print 50 x as the separator include math # include a module (can be understood as reference namespace in C #) puts SQRT (2) # Here we call the SQRT method puts "*" * 50 puts me: piinclude meputs SQRT (2) # Here we call the SQRT method puts SQRT (1, 2) in me) # Same as puts piputs "*" * 50 puts Math: SQRT (2) # Call the SQRT method puts math in math through a complete reference. SQRT (2) # same effect as above puts "*" * 50
Running result:
3.14159265358979
1.4142135623731
**************************************** **********
1.4142135623731
**************************************** **********
3.14
Num1 = 2, num2 =-1
Num1 = 1, num2 = 2
3.14
**************************************** **********
1.4142135623731
1.4142135623731
**************************************** **********
In addition, the module can be used to achieve effects similar to "interfaces". For example, there is a scenario:
In an animal game, there are n kinds of ducks. All ducks can swim, but wild ducks can fly. According to the traditional OO approach, we will define a duck base class, then all the ducks inherit from it, and then get another ifly interface so that the "wild duck" class can implement this interface.
Ruby can do this:
# Duck class duckdef swimprint self. class, "can swim... \ n "; endend #" Flying "module flymoduledef flyprint" and I can fly... \ n "endend # Wild Duck (flying, traveling) Class Mallard <duckinclude flymodule # After importing the module, this class has the method defined in the module (which can be understood as implementing the interface) end # black duck (games only) Class coot <duckdef color "black" endendacoot = Coot. newaco. swim; amallard = Mallard. newamallard. swim; amallard. fly;
Running result:
Coot can swim...
Mallard can swim...
And I can fly...
Finally, the module can play something that the static language thinks bt is. For example, in this game, the system randomly drops the next baby from the sky, who in a group of black ducks can fly after they find it! This is hard not to mention Ruby:
Acot1 = coot. newacot2 = coot. newacot2.extend (flymodule) acot1.swimacoot2. swimacoot2.fly # acot1.fly # Because acot1 does not extend the flymodule, it cannot fly and the call will report an error
Here, the instance acot2 extends the flymodule module through the extend keyword, so this instance can call the methods in the flymodule!