Ruby Seven-Day primer (3 Mixin, collections, file operations)

Source: Internet
Author: User

Day 3

Learning to enter the third day, today plans to learn modules, collections and Simple file operations.

3.1Mixin Module

Object-oriented languages use inheritance to propagate behavior to similar objects. Specific to the language, C + + uses a multi-inheritance, but too complex, Java uses interface, and Ruby chooses to use the module, first look at the code:

Module ToFile def filename"Object_#{self.object_id}.txt"    Enddef To_ffile= File.Open(FileName,' W ')file.Write(to_s)EndEndClass PersonincludeToFile attr_accessor:name def Initialize (name) @name = NameEnddef to_s NameEndEnda= person.New(' Lee ') putsa. Name,a. to_s,a. to_f

We define a ToFile module and include it in the class person (it feels a bit like an include in C + +).

    • This Include,person class directly has the ability to tofile, and they establish a relationship that allows the method inside the person class to be called in ToFile. This plug-and-play approach is obviously quite flexible by defining the main parts of the class first and then adding additional capabilities through the module, which we call mixin.
3.2 Integrated operation

There is nothing special about collection usage, but the use of some methods:

Puts' Begin '<=>' End 'a=[4,5,2,1]a=a.SortPutsaPutsa. any? {|i| i>6}putsa. All? {|i| i>0}putsa. collect {|i| i*2}putsa. Select {|i|i%2==0}putsa.MaxPutsa. Member? (2)
    • <=> is called the spaceship operator, it compares a, b two operand, b large return -1,a large return 1, equal return 0.
    • Returns true if any of the element conditions in the collection are true, and only if all the elements of the collection are true.
    • The sort method can be called as long as the corresponding class implements the spaceship operator.
    • The Collect and map methods apply the function to each element and return the result array
    • The Find method finds an element that meets the criteria, and both the Select and Find_all methods return all eligible elements.
3.3 File Operations

We have already used the file write in 3.1, and the file read is simple:

File.open("tree.rb""r"do |file|    file.each_line("\r\n"){|line"#{line.to_s}"}end

The above code uses code blocks, and if you do not use code blocks, you need to close the file manually.

file = File.open("tree.rb""r"file.each_line("\r\n"){|line"#{line.to_s}"}file.close

We can also use the simpler IO classes:

IO.foreach("tree.rb"){|lineline}
3.4 Practice Today

Write a simple grep program that prints out the lines of a phrase in the file and loses the travel number.

def grep(filename,match)    line_num=0    IO.foreach(filename)do|line|         line_num=line_num+1        puts line_num.to_s+" "+lineifline.match(match)    end endgrep("tree.rb",/[Tt]ree/)

Ruby Seven-Day primer (3 Mixin, collections, file operations)

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.