Ruby study note 4

Source: Internet
Author: User
Document directory
  • Ruby Module
  • Mix-in
  • Require and Load
  • Module Summary
  • Array
  • String
  • Regular Expression
  • Iterator, code block, Closure

 

Ruby Module

Ruby uses module... end to define a module. The module provides a namespace to prevent name conflicts.

    module Me
def foo()
return 0;
end
end

To use the functions in the me module, you can:

    include Me
foo()

If multiple consecutive inclde modules have functions with the same name, the preceding functions will be overwritten by the include functions.

The standard library provides many modules, and the math module provides a large number of similar methods, such as math. SQRT. The preceding modules use the include module name and then call the method.
Implemented. This include is a bit like using in C ++.
Namespace, sometimes we want to use it without include, just like math. SQRT, because it can prevent multiple modules of include from being named differently.
Status. In this case, we need to define the function as a module function.

    module Me2
def Me2.foo()
end
end

In the following example, me2.foo () can be called without the need to include me2.

You can access not only functions in the module, but also constants in the module. The constant access method is module: constant name.

Mix-in

You can put all the functions defined in a module into a class.

    class Person
include Me
end

In this way, the Foo () method is available in the person class.

    p = Person.new
p.foo()

There is also an extend method corresponding to the include method. Include is a method to add this module to the entire class. Extend can add modules to a single instance of a class.

    p1 = Person.new
p1.extend(Me2)
Require and Load

Similar to include in C ++, require is used to include another file. The difference is:

Require is loaded only once and will be ignored when the same file is encountered, but files with the same name in different paths will be loaded multiple times.

Load will be loaded multiple times, as long as the load statement is run, it will be loaded once. Therefore, using the load feature, you can achieve seamless system upgrade. When the program function is changed, you only need to load it again.

Module Summary

Require, load is used to load files, include, extend is used to contain modules.

Require is loaded once and load is loaded multiple times.

Require can load files without a suffix, and load must be followed by a suffix.

Require is generally used to load library files, and load is generally used to load configuration files.

Array
    arr1 = []
arr2 = Array.new
arr3 = [4, 5, 6]

The array in Ruby uses an integer as the subscript. The subscript starts from 0 and is consistent with the C language. You can use a negative subscript to indicate that the index starts from the end. For example, arr3 [-1] indicates the last element, which is 6 in the above example.

You can use a number to index the array. The first number indicates the start position and the second number indicates the number. Therefore, arr3 [1 .. 2] indicates [4, 5].

Arrays in ruby are dynamic. You can add or delete elements at any time.

Arr3 [6] = 45; # Set the element of index 6 to 45, index 3, 4, and 5 to nil.
Arr3.delete _ at (3) # delete an element whose subscript is 3
Arr3.delete (5) # delete all elements with a value of 5
Arr3.insert (3, 66) # add element 66 at the position marked as 3
Arr. Pop # Delete the last element
Arr. Shift # Delete the first element
Arr. Clear # clear an array
Print arr. Join (",") # Join the array into a string input. Each element is separated by commas (,).
String
    str1 = 'str'
str2 = "str"
str3 = %q/str/
str4 = %Q(str)
str5 = <<OK_str
dksla;f
str
OK_str

% Q is used to generate a single quotation mark string. % Q is used to generate double quotation mark strings. % Q and % Q are followed by delimiters. They can be paired! !; //; <>; (); []; {}; And so on

Str5 is a string document. The actual content of str5 is

        dksla;f
str

You can use escape characters similar to the C language, such as "/N", or use # {} to embed a ruby expression. The expression is evaluated and connected to the string.

Regular Expression iterator, code block, Closure
    (1..9).each{|i| print i if i < 7}

The iterator each is a method of the array class. The code in {} is a code block. In addition to {}, you can also use do... end to organize code blocks. {} Has a higher priority than do... end.

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.