Ruby on Rails

How to Use The Kernel Open Metho

2020-09-03  本文已影响0人  jackli0855

Ruby has a very flexible open method.

It allows you to open files & read data from a URL, and even open pipes to run external commands.

Example:

open "abc.txt"

That's why this method is to be avoided.

It's a security risk that it can do all these things.

Instead, it's better to use the specific open method that you need.

Example:

File.open           # File
IO.popen            # Process open
URI.parse(...).open # URL open

All of these return an IO object.

Summary

You've learned about input/output in Ruby! You've learned about file descriptors, the IO class, buffering, reading in chunks & standard IO streams.

ruby-io-mindmap.png
上一篇 下一篇

猜你喜欢

热点阅读