Ruby openuri Module

Source: Internet
Author: User
To obtain the content of an HTTP/FTP address in Ruby, you can easily use the open-Uri library.

Open-Uri library is included in Ruby's standard additional library, path: lib/oepn-uri.rb

After referencing this additional library, you can open the resource of an HTTP/ftp uri just like opening a common file.

The difference is as follows: Require ' Open-Uri '

Open ( ' C:/boot. ini ' ){ | File |
Puts file. Class # File
}

Open ( ' Http://coderlee.cnblogs.com ' ){ | File |
Puts file. Class   # Tempfile
}

It can be seen that the block parameter for opening a local file is of the file type, while the block parameter for opening a URI is of the tempfile type.

Note that the open method in Open-Uri will automatically track access to the 302 redirection address, while the net: HTTP # Get method will not automatically track the 302 redirection address.

the final value returned by the function is the final value of the passed Code block. You can obtain the URI content data = open ( ' http://coderlee.cnblogs.com ' ) { | F | F. read
data = open ( ' http://coderlee.cnblogs.com ' ) { | F | F }. open. read

the reason for the second open statement is that the stream returned by the open method is a closed tempfile. Therefore, open the stream first, return the opened stream, and then call read to read the stream.

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.