Interesting ruby-Study notes 4

Source: Internet
Author: User

Ruby block, in my opinion, is inserting a variable function.
block_name{   statement1 statement2 .....   }

It doesn't seem to know what it is, but don't worry, keep looking down. Block function calls yield statement by yield the English is yielding, giving up, do not know why use this word, is this function to give up control right? Example
#!/usr/bin/ruby#-*-coding:utf-8-*-def test   puts "in test method"   yield   puts "you're back in the test method"   yieldendtest {puts "You are inside the Block"}
After running this paragraph, the effect is
Within the test method you are inside the block and you're back inside the test method.
In the yield section you run the BLOCK statement that you passed in when you called. So does yield look useless? Keep looking down.
Yield can take parameters you can also pass a yield statement with parameters. Here is an example:
#!/usr/bin/ruby#-*-coding:utf-8-*-def test   yield 5   puts "within test method"   yield 100endtest {|i| puts "you are in Block #{i} Inside "}

Blocks and methods if the last parameter of the method is preceded by a &amp, then you can pass a block to the method, and the block can be assigned to the last parameter. If * and & appear in the parameter list at the same time,& should be placed in the back.
#!/usr/bin/rubydef Test (&block)   block.callendtest {puts "Hello world!"}
Does it remind you of the callback function in JavaScript?In combination with the yield parameter, a callback function can be passed in, and the callback function can make different behavior according to the different parameters passed in the function execution process. finally feel the block this feature is a bit of use ...
The begin and end blocks begin and end blocks are like the interceptors in Java, one is the before interceptor, the other is the after interceptor
#!/usr/bin/rubybegin {   # begin code block  puts "begin code block"} END {   # end code block  puts "end code block"}  # MAIN code block puts "M AIN code block "
A program can contain multiple BEGIN and END blocks. The BEGIN block executes in the order in which they appear. The END blocks are executed in the reverse order in which they appear. When executed, the above program produces the following results:

BEGIN code block main code block end code block
The Ruby module module is a way to combine methods, classes, and constants. Module gives you two great benefits
    • The module provides a namespace and avoids name collisions
    • The module realizes the mixin device
Module defines a namespace equivalent to a sandbox in which your methods and constants do not conflict with method constants elsewhere.
    • Modules are similar to classes, but there are different modules that cannot be instantiated
    • Module has no subclasses
    • Modules can only be defined by another module
Module Identifier   statement1   statement2   ..... end
module constant naming is similar to class constant naming, starting with uppercase letters。 The method definition looks similar: the module method definition is similar to the class method definition.
Example

#!/usr/bin/ruby# defined in the TRIG.RB file modules module trig   PI = 3.141592654   def trig.sin (x)   #:   End   def trig.cos (x)   #:   EndEnd

Require statements finally saw the require statement.! No require function is simply not able to write code Ah, so combined with the Require,module function is I see the most important features of the example
$LOAD _path << '. ' Require ' trig.rb ' y = Trig.sin (trig::P I/4)
Note This sentence $LOAD _path << '. ' This sentence is the path of the require to the current file path, I just started require always fail because there is no such sentence if you do not want to use the $LOAD _path can also use the Require_relative method
Require_relative ' trig.rb ' y = Trig.sin (trig::P I/4)

Or you can! And I prefer require_relative because it's better to remember.!

Include statements you can embed modules in a class. You must have the same question with me: "But I have require why do I have to include?! ”

Suppose the following code is written in SUPPORT.RB
Module Week   first_day = "Sunday"   def week.weeks_in_month      puts "you had four weeks in a month"   end   de F week.weeks_in_year      puts "you had a weeks in a year"   EndEnd

Let's embed it.
#!/usr/bin/ruby$load_path << '. ' Require "Support" class Decadeinclude Week   no_of_yrs=10   def no_of_months      puts Week::first_day      number =10*12      puts number   endendd1=decade.newputs week::first_dayweek.weeks_in_monthweek.weeks_in_yeard1.no_of_ Months
You will find thatThere's no difference between that line include Week code execution results! What kind of eggs does the include have?! To explain what the include is all about, let's introduce the Ruby Mixins feature in Ruby, there's no multiple inheritance in Mixinsruby, instead of mixin. When you include the module in the class definition, the method in the module is mix into the class instance code, see A, B how to mix into the sample inside
Module A   def A1   End   def a2   endendmodule B   def B1   end   def B2   Endendclass Sampleinclude ainclude B   def s1   endendsamp=sample.newsamp.a1samp.a2samp.b1samp.b2samp.s1

Include & Require & Load
The original include and require have the following differences (this way also refer to the Load method)
    • Require does not need to follow the suffix, it will automatically recognize XXX.RB
    • Require if the call 2 times will be an error, if you want to call multiple times with load, but with load to write the file suffix name
    • Require generally used to load library files, load general user load profiles
    • Include is used to mix modules in a file into a class
    • The include does not copy the instance method of the module to the class, only the reference, and the different classes that contain the module point to the same object. If you change the definition of module, even if your program is still running, all classes containing module will change behavior

Interesting ruby-Study notes 4

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.