Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

Ruby Programming/Reference/Objects/IO/File

From Wikibooks, open books for an open world
<Ruby Programming |Reference |Objects |IO
This page may need to bereviewed for quality.

File

[edit |edit source]

The file class is typically used for opening and closing files, as well as a few methods like deleting them and stat'ing them.

If you want to manipulate a file, not open it, also check out the Pathname class, as well as the FileUtils class.

To create a directory you'll need to use the Dir.mkdir method.

File#chmod

[edit |edit source]

Here's how to do the equivalent of "chmod u+x filename"

File.class_eval do   def self.addmod(flags, filename)     themode=stat(filename).mode | flags     chmod(themode, filename)   endend

Although there's no error checking and the return value could probablybe something better (like themode).

So after defining that, your 'u+w' would be:@File.addmod(0200, 'filename')@fromhttp://www.ruby-forum.com/topic/196999#new

File#grep

[edit |edit source]

This is actually Enumerable#grep, and I believe it just works piece-wise, likeFile.each_line{|l|yield l if l=~ /whatever/ }

File.join

[edit |edit source]

This is useful for combining objects that aren't (or might not be) strings into a path. Seehere.

Retrieved from "https://en.wikibooks.org/w/index.php?title=Ruby_Programming/Reference/Objects/IO/File&oldid=1845504"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp