Menu
Tempfile’s filename picking method is both thread-safe and inter-process-safe: it guarantees that no other threads or processes will pick the same filename. Tempfile itself however may not be entirely thread-safe. If you access the same Tempfile object from multiple threads then. Report or block benfoxall. Hide content and notifications from this user. Learn more about blocking users. Contact Support about this user’s behavior. Learn more about reporting abuse. Report abuse View GitHub Profile Sort: Least recently created. Sort options.
Ruby provides a whole set of I/O-related methods implemented in the Kernel module. All the I/O methods are derived from the class IO.The class IO provides all the basic methods, such as read, write, gets, puts, readline, getc, and printf.This chapter will cover all the basic I/O functions available in Ruby. For more functions, please refer to Ruby Class IO. The puts StatementIn the previous chapters, you have assigned values to variables and then printed the output using puts statement.The puts statement instructs the program to display the value stored in the variable.
This will add a new line at the end of each line it writes. #!/usr/bin/rubyval1 = 'This is variable one'val2 = 'This is variable two'puts val1puts val2This will produce the following result −This is variable oneThis is variable twoThe gets StatementThe gets statement can be used to take any input from the user from standard screen called STDIN. ExampleThe following code shows you how to use the gets statement. This code will prompt the user to enter a value, which will be stored in a variable val and finally will be printed on STDOUT.#!/usr/bin/rubyputs 'Enter a value:'val = getsputs valThis will produce the following result −Enter a value:This is entered valueThis is entered valueThe putc StatementUnlike the puts statement, which outputs the entire string onto the screen, the putc statement can be used to output one character at a time. ExampleThe output of the following code is just the character H −. #!/usr/bin/rubyprint 'Hello World'print 'Good Morning'This will produce the following result −Hello WorldGood MorningOpening and Closing FilesUntil now, you have been reading and writing to the standard input and output. Now, we will see how to play with actual data files.
The File.new MethodYou can create a File object using File.new method for reading, writing, or both, according to the mode string. Finally, you can use File.close method to close that file. SyntaxaFile = File.new('filename', 'mode')#. Process the fileaFile.closeThe File.open MethodYou can use File.open method to create a new file object and assign that file object to a file. However, there is one difference in between File.open and File.new methods. The difference is that the File.open method can be associated with a block, whereas you cannot do the same using the File.new method.File.open('filename', 'mode') do aFile #. Process the fileendHere is a list of The Different Modes of opening a File −.
puts Time.now - t1I let it run for 20 minutes with no output. (A tiny file works fine.) I triedthe same program using read(65000) instead of gets, just to see if the line sizewas the problem. I stopped it after 20 minutes also.So I tried it in Dolphin, with this program:start:= Time totalSeconds.stream:= FileStream read: 'c:/data/ruby/sets/data.txt'.stream atEnd whileFalse: line = stream nextLine.end:= Time totalSeconds.Transcript show: (end - start) printString; crIt runs in 178 seconds.
Three minutes. What's up with that?
Am I missingsomething? I'm going to go let Ruby finish this job no matter how long it takes.I'll report the total time when I get it.Is it me? Or is it Ruby? Thanks!Ronald E Jeffriesgiving the best advice I have. You get to decide whether it's true for you.Mon, 23 Aug 2004 20:13:15 GMT. Hi,At Thu, 7 Mar 2002 21:18:40 +0900, So I'm doing this benchmark to work with my set program. Part of the problem is to read a six million record file.
So I built one, six million 64-byte lines plus newline, didn't seem to take that long. Then I decided to time how long it would take to read, with this program:IO in ruby 1.6 is slow.
And you look run it under Windows, 1.6mswin/mingw is.VERY. slow. Try with 1.7. I let it run for 20 minutes with no output. (A tiny file works fine.) I tried the same program using read(65000) instead of gets, just to see if the line size was the problem. I stopped it after 20 minutes also.In 1.7, IO#read is much faster than IO#gets.
Your example runin 101sec, 49sec with read(65), and 19sec with read(65000).On over 300 megs of file? It takes Windows six minutes to copy it. What are youusing for hardware / software?Hmm, I'm so confused.I wonder how hard it would be to get 1.7 running on Windows without Andy'swonderful package.Thanks,Ronald E Jeffriesgiving the best advice I have. You get to decide whether it's true for you.Mon, 23 Aug 2004 22:43:32 GMT. In 1.7, IO#read is much faster than IO#gets. Your example runin 101sec, 49sec with read(65), and 19sec with read(65000).RJ On over 300 megs of file?
It takes Windows six minutes to copy it. What are youRJ using for hardware / software?But you're only reading, not writing. 19 seconds is in the realm ofpossibility. Here is an example:$ ls -l 300megs.txt-rw-r-r- 1 joe joe 300000000 Mar 7 07:38 300megs.txt$ time cat 300megs.txt /dev/nullreal 0m3.733suser 0m0.211ssys 0m2.931s3.7 seconds to read the file. This is on a pentium 500 runningFreeBSD with 2 scsi disks striped. The system is fairly busy servingup web and crunching data.-joeMon, 23 Aug 2004 23:48:05 GMT.
In 1.7, IO#read is much faster than IO#gets. Your example runin 101sec, 49sec with read(65), and 19sec with read(65000).RJ On over 300 megs of file? It takes Windows six minutes to copy it.
What are youRJ using for hardware / software?JM But you're only reading, not writing. 19 seconds is in the realm ofJM possibility. Here is an example:JM $ ls -l 300megs.txtJM -rw-r-r- 1 joe joe 300000000 Mar 7 07:38 300megs.txtJM $ time cat 300megs.txt /dev/nullJM real 0m3.733sJM user 0m0.211sJM sys 0m2.931sJM 3.7 seconds to read the file. This is on a pentium 500 runningJM FreeBSD with 2 scsi disks striped.
The system is fairly busy servingJM up web and crunching data.Oh I forgot to time a copy:$ time cp 300megs.txt 300megs.txt.copyreal 0m34.761suser 0m0.015ssys 0m5.588sso it's about 9 times faster than your 6 minutes, so perhaps you couldmultiply my 3.7 seconds read time by 9 to get 33 seconds 'theoreticalmax' read time for your setup? Or just take it as anotherdatapoint.:-)-joeTue, 24 Aug 2004 00:04:09 GMT. In 1.7, IO#read is much faster than IO#gets. Your example run in 101sec, 49sec with read(65), and 19sec with read(65000). On over 300 megs of file? It takes Windows six minutes to copy it. What are you using for hardware / software?Just FAT fs is silly.
Not so great system.CPU: Intel Celeron (Mendocino) 500MHz x 2MEMORY: 384MBOS: Linux 2.4.18-pre9File System: ReiserFS version 3.6.25Ruby: 1.7.2 20020307$ ls -l data.txt-rw-r-r- 1 nobu nakada 390000000 Mar 7 22:27 data.txt$ time wc data.txt60000 data.txtreal 0m18.588suser 0m6.790ssys 0m3.390s$ time ruby17 t1.rb109.646086real 1m49.744suser 1m40.910ssys 0m4.280s$ time ruby17 t2.rb46.189003real 0m46.279suser 0m38.370ssys 0m3.950s$ time ruby17 t3.rb17.898967real 0m17.996suser 0m2.790ssys 0m3.940st1.rb is your code, t2.rb and t3.rb use read(65) andread(65000) respectively. I'm not sure why t3.rb faster thanwc.-Nobu NakadaTue, 24 Aug 2004 00:11:44 GMT.