· KLDP.org · KLDP.net · KLDP Wiki · KLDP BBS ·
Humble Little Ruby Book/Chap4

ÃÖ±Ù ¾ð¾îµéÀÇ ¶óÀ̺귯¸®¿¡¼­ ÀÌ·ç¾îÁö°í ÀÖ´Â ÀÛ¾÷Àº »ç¶÷µéÀÌ PerlÀ̳ª Bash script ´ë½Å¿¡ PythonÀ̳ª Ruby ¿Í °°Àº ´õ Ä£±ÙÇÑ ¼Ö·ç¼ÇÀ¸·Î ó¸®ÇÒ ¼ö ÀÖµµ·Ï ÇÏ´Â °ÍÀÔ´Ï´Ù. ±×·± ¾ð¾îµéÀ» »ç¿ëÇÏ¸é ´õ ÀÛÀº ³ë·ÂÀ¸·Îµµ °°Àº ÀÛ¾÷À» ¼öÇàÇÒ ¼ö Àֱ⠶§¹®ÀÔ´Ï´Ù. ±× ¶óÀ̺귯¸®µé ´öÅÿ¡ Ruby´Â ±×·± ³­ÇØÇÑ ¼Ö·ç¼Çµé(Perl, Bash µî) ¸¸Å­ ½Ã½ºÅÛ°ú »óÈ£ÀÛ¿ëÀ» ÇÒ ¼ö ÀÖ½À´Ï´Ù. ±×·³ ±× ½Ã½ºÅÛ ¶óÀ̺귯¸® ÀÏºÎ¿Í ·çºñÀÇ ³»Àå ÇÔ¼ö¿¡ ´ëÇؼ­ »ìÆ캾½Ã´Ù. ¾Æ¸¶ ´ç½ÅÀº 1½Ã°£ ³»¿¡ Perl ¸Å´º¾óÀÇ º¹»çº»À» ÆȾÆÄ¡¿ö¹ö¸®°Ô µÉ °ÍÀÔ´Ï´Ù!

1. ÆÄÀϽýºÅÛ

RubyÀÇ File Ŭ·¡½º´Â ´Ù¸¥ ºñ½ÁÇÑ ¼º°ÝÀ» Áö´Ñ ¾ð¾î(ex. Python)¿Í ºñ±³ÇÏ¸é ¸Å¿ì °­·ÂÇÕ´Ï´Ù. ´õ ¸¹Àº ¸Þ¼Òµå¸¦ °¡Áö°í ÀÖÀ» »Ó ¾Æ´Ï¶ó ³í¸®ÀûÀ¸·Î ´õ ÀûÇÕÇÑ À̸§À» °¡Áö°í ÀÖ½À´Ï´Ù.(e.g. Python ¿¡¼­ unlink °¡ ÇÏ´Â ÀÏÀÌ ¹¹Áö? delete? ±×·³ ¿Ö ±×·¸°Ô ºÎ¸£Áö ¾Ê´Â°Å¾ß!?). RubyÀÇ File Ŭ·¡½ºÀÇ °­·ÅÇÔ°ú »ç¿ëÀÇ Æí¸®ÇÔÀ» ´Ù¸¥ ¾ð¾îµé°ú ºñ±³ÇÏ¸é ´«¿À´Â ³¯¿¡ Ŭ·¡½Ä À½¾Ç°ú ÇÔ²² µû¶æÇÑ ½ºÇÁ¸¦ ¸Ô´Â °Íó·³ ´õ Æí¾ÈÇÒ °Ì´Ï´Ù.

¿ì¼±, ÇϳªÀÇ ÆÄÀÏ¿¡ °üÇÏ¿© ¾Ë¾Æ³¾ ¼ö ÀÖ´Â °Í¿¡ ´ëÇؼ­ »ìÆ캾½Ã´Ù. ÆÄÀÏÀÌ Á¸ÀçÇϴ°¡? ¾î¶² Á¾·ùÀÇ ÆÄÀÏÀΰ¡? ±×°Ç ÆÄÀÏÀÌ ¸Â´Â°¡? ¾Æ·¡¿¡ ¸î°³ÀÇ ¿¹Á¦°¡ ÀÖ½À´Ï´Ù.("textfile.txt" °¡ ÇöÀç µð·ºÅ丮¿¡ ÀÖ´Ù°í °¡Á¤ÇÕ´Ï´Ù.)
File.directory?("textfile.txt") ¡æ false
File.file?("textfile.txt") ¡æ true
File.exists?("textfile.txt") ¡æ true
File.size?("textfile.txt") ¡æ 2063
File.extname("textfile.txt") ¡æ ".txt"
File.extname("igotnoextension") ¡æ ""
À§ÀÇ °ÍµéÀÇ Àǹ̿¡ ´ëÇؼ­´Â ÀÚ¼¼ÇÑ ¼³¸íÀº ÇÏÁö ¾Ê°Ú½À´Ï´Ù. ´ç½ÅµéÀÇ Áö´ÉÀ» ÀǽÉÇÏ±ä ½ÈÀ¸´Ï±î¿ä :). ÇÏÁö¸¸ µÎ°¡Áö´Â ¤°í ³Ñ¾î°¡°Ú½À´Ï´Ù. ¿ì¼± "size?" ¸Þ¼Òµå´Â kilobytes°¡ ¾Æ´Ñ bytes »çÀÌÁ ¸®ÅÏÇÕ´Ï´Ù. µÎ¹ø°·Î´Â "size?" ¸Þ¼Òµå´Â ÆÄÀÏÀÇ size°¡ 0À϶§ nil À» ¸®ÅÏÇÕ´Ï´Ù.

¶ÇÇÑ File Ŭ·¡½º´Â ÆÄÀÏ¿¡ ´ëÇÑ ¼ÒÀ¯±Ç(ownership)°ú Çã°¡±Ç(permission)°ú °°Àº ¸ÞŸµ¥ÀÌÅ͵鿡 ´ëÇÑ Á¤º¸¸¦ Á¦°øÇÕ´Ï´Ù.
File.executable?("textfile.txt") ¡æ false
File.readable?("textfile.txt") ¡æ true
File.writable?("textfile.txt") ¡æ true
File.owned?("textfile.txt") ¡æ true
File.grpowned?("textfile.txt") ¡æ false
File.setgid?("textfile.txt") ¡æ false
File.setuid?("textfile.txt") ¡æ false

The executable? (which determines if the user has the ability to execute a file according to filesystem permissions, not whether or not the file is an executable), readable?, and writable? methods have companion methods called executable_real?, readable_real?, and writable_real? (respectively, obviously) which make sure that the owner of the process has that ability with that file. Of course, if you own the file it probably doesn't matter. You can find out if you own it using the owned? method, which will return true if the process owner indeed owns the specified file. Normally the grpowned?, setgid?, and setuid? are very helpful in finding out certain metadata about a file, but these methods don't apply to and will always return false on operating systems that don't support them (I'm looking right at you Windows!). For those not in the know, on UNIX filesystems a file is owned by a user in a group rather than "just" a user; the grpowned? gains you access to this data. The setgid? and setuid? check for a bit that is set on a file's filsystem entry that allows you to change the user and/or the group when accessing that file (this helps when a user needs elevated privileges for a certain task). Again, these methods allow you to see if these bits are set, but if you're on Windows or something else that doesn't support them then they always return false

1.1. ÆÄÀϷκÎÅÍ Àоî¿À±â

I can hear you saying, "Who cares about that crap?! I need to read a file. I made the file. I know all that crap about it! Tell me how to read it or I challenge you to a knife fight, right now, behind the Waffle House! You and me, pal! We're taking it to the matresses!" I would like to now kindly respond to your anger with this little tidbit:
myfile = File.open("textfile.txt", "r")
myfile.each_line {|line| puts line }
myfile.close
Using the File#open method, you can open a file and create a new File instance. The first parameter for open is the file path (either relative or absolute), and the second parameter is the file mode. You can view the table of options you have for this parameter in the table at the end of this section; this parameter defaults to reading if you don't specify. After you call open, you can use the each_line method to grab each line and print it out, play around with it, whatever you want to do inside the block. You can optionally feed each_line a parameter that will act as the line ending in place of "\n"; if you, like me, tend to end each line of text with the word "pastry" you can respect this feature. Always be sure to call the close method if you are opening files this way.

"But, Dad!" you whine. "I don't wanna call close!" Well, Son/Daughter/Androgynous Offspring, Ruby can help you cure your incessant moaning:
File.open("textfile.txt") do |myfile|
myfile.each_line {|line| puts line }
end
This does the same thing, but now the file stream is automatically closed when the enclosing block exits. "Wow!" you exclaim. I'm glad you're amazed, but it gets better:
IO.foreach("textfile.txt") {|line| puts line }
Using the IO#foreach method does the same thing as the previous two examples, just simpler, more compact, and far more beautifully. It opens the file specified, feeds it line by line into a block, then closes it. Mmm...now that's Rubylicious.

1.2. ÆÄÀÏ¿¡ ¾²±â

Your options for writing to a file are numerous; they all accomplish essentially the same objective but in slightly different ways. The first (and most obviously named) way I'd like to cover is the write method. It goes something like this:
File.open("textfile.txt", "w") do |myfile|
myfile.write("Howdy!")
end
You open a file with the File#open method, create an enclosing block, and simply call the write method on the file instance created by the block. You can do writing the same way I showed you reading the first time (i.e. without a block at all and calling close), but I thought that would be needlessly redundant to include it here. You can write any sort of data to a file as long as it can be converted to a string (i.e. it has a to_s method); if it can't be converted to a string Ruby will simply issue it a string representation to the effect of "#<ClassName:SomeData>". Other methods such as print and puts can easily be plugged into where write is; they take the same number of parameters and behave essentially the same way (except that puts will tag a new line on the end of the string when it is written).

Another way of writing to a file is utilizing the << operator; if you've ever used IOStream in C++ then you should feel right at home with this:
File.open("textfile.txt", "w") do |myfile|
myfile << "Howdy!\n" << "There are " << count << "pandas!"
end
Opening the file is the same as always, but now instead of calling a method and feeding in parameters (at least in the traditional sense) you are now using the << operator. It behaves the same as the other methods (i.e. it converts the data to a string if it is not a string and writes it to the file) so there shouldn't be any surprising parts there. BOOGABLARGABOO! Okay, maybe that surprised you, but nothing else should.

1.3. ´õ ¸¹Àº ÆÄÀÏ ÀÛ¾÷

The File class also supports a number of other file operations that promote all sorts of filesystem hooliganism. Here are a few:
File.delete("textfile.txt")
File.rename("textfile.txt", "textfile.txt.bak")
File.chown(nil, 201, "textfile.txt")
File.chmod(0777, "textfile.txt")
The first two method's names should give away their function. If the proverbial cat is not out of the bag, they delete and rename the provided files (with the renamed filename fed in as the second parameter). The delete method will return the number of files deleted (i.e. 1 for this case).

The last two methods may be a little confusing if you are not up to snuff on your UNIX/Linux filesystems and their associated commands. The chown command allows a user with superuser privileges to change the owner of a file (or the owner may change the group ownership to any group of which he/she is a member); note that the chown method takes numeric owner and group IDs rather than string names (which the command line version allows). The chmod method allows the owner of a file (or a superuser) to change the permissions of a file (i.e. which users/groups can read, write to, or execute a file); the first parameter is a bit pattern which represents the permissions on the filesystem. Check Appendix A for URLs with more information on UNIX filesystem metadata (including bit patterns to be used with the chmod method).

File Access Modes
rRead-only access; starts at beginning of file (default)
wWrite-only; truncates existing file to zero length or creates new file
aWrite-only; starts at end of existing file or creates new file
r+Read-write; starts at beginning of file
w+Read-write; truncates existing file to zero length or creates new file
a+Read-write; starts at end of existing file or creates new file
b Binary file mode; may appear with any of the above options (Windows only)

2. Thread¿Í Folk¿Í Process

2.1. Ruby ¾²·¹µå ±âº»

2.2. ¾²·¹µå Á¦¾îÇϱâ

2.3. ¾²·¹µå¿¡ ´ëÇØ ÀÚ¼¼È÷ ¾Ë±â

2.4. ÇÁ·Î¼¼½º

3. ȯ°æ¼³Á¤

3.1. Environment variables and the like

3.2. The command line and you

3.3. Ruby and its little corner of your computer

4. Win32 and Beyond

4.1. API

4.2. ·¹Áö½ºÆ®¸®

4.3. OLE Automation

5. ÀÌ Àå¿¡¼­´Â..


ID
Password
Join
Stop searching forever. Happiness is unattainable.


sponsored by andamiro
sponsored by cdnetworks
sponsored by HP

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2007-01-21 20:49:50
Processing time 0.0063 sec