shitou's blog 
Home About Feed | 十人族 MIX BOY 塌客

Category "ruby"

23
Jul

[转载]Using temporary files in Ruby - Tempfile.new by shitou

0

一个创建临时文件的ruby内置class

For certain programming solutions, you might need a temporary file. Different operating systems store temporary files in different locations. Also you don’t want to explicitly name the file since that is irrelevant in the program. How do you ensure that there are no filename conflicts? The solution for all these is to use library for temp file processing.

Ruby comes a with a default library ‘tempfile’ for handling temporary file creation. Given a filename prefix, it creates a temporary file in the following format.

[prefix]-[process].[unique_number]

This ensures that there is no conflict in creating multiple temporary files in the same directory. Once your program terminates the temporary file is automatically deleted. Also by default, the temporary file is created in the operating system’s temporary folder (In my ubuntu system it is /tmp).

In the following program, a temporary file with prefix ‘random’ to store 50 random numbers is created. When the program terminates you cannot see the temp file. Hence I have added a ‘gets’ which stops for user input. In my case I could see a temporary file named ‘random.5789.0′ under \tmp dir.

 

  1. require 'tempfile'  
  2.   
  3. class TempSample  
  4.   
  5. temp_file = Tempfile.new('random')  
  6. 1.upto(50) do  
  7.   num = rand(50) # random number generation in ruby  
  8.   temp_file.print(num,"\n") # print number and newline to the file  
  9. end  
  10. temp_file.flush # flush it so that you can see it using cat  
  11.   
  12. gets # wait for user input. At this point you can see a temp file /tmp  
  13. # The filename in my case was random.5789.0  
  14.   
  15. end  

require 'tempfile'  class TempSample  temp_file = Tempfile.new('random') 1.upto(50) do   num = rand(50) # random number generation in ruby   temp_file.print(num,"\n") # print number and newline to the file end temp_file.flush # flush it so that you can see it using cat  gets # wait for user input. At this point you can see a temp file /tmp # The filename in my case was random.5789.0  end  

If you want to the temp file in a specific directory, you can pass the directory name as the second parameter to new. For example - Tempfile.new(’new1′,’/usr/home/jay’)

Tags: ruby

2008-07-23 10:35:52, 862 reviews

send to mailbox

Your email:

Related Posts

[转载]5个有用的ruby gems

关于yield的使用

进程和线程

ruby写的一个随机发扑克类

服务器监控小脚本

Ruby和Erlang的交互

0 Responses

 

TOP

Tags

tenerer MooseFS gearman-ruby Gearman MongoDB MochiChat TCP U-ka saegusa IN db command Mai Kuraki Norah Jones log iPhoneException ACG Mac Safari objective-c CouchDB LVS AJAX debian 推荐 AMQP google mail bug gettext Erlang 北京 iptables 架构 tips mysql backup function 我看 postfix 监控 SEO cache Etag memcache thread 进程 线程 无锡 yield file column mixboy xml rss gems ruby shitou shell lighttpd 安全 csrf 公司 nginx linux 模块 apache webserver 朋友 大学 生活 尼古拉斯凯奇 movie 文件同步 笑笑 歌词 auto complete plugin rails music ubuntu blog

Category

  • iPhone[17]
  • Erlang[8]
  • google[9]
  • 生活[41]
  • 音乐[11]
  • 电影[11]
  • linux[24]
  • web server[6]
  • mail server[3]
  • cluster[1]
  • system manage[8]
  • ruby[18]
  • ruby on rails[27]
  • 开源[3]

Episode

  • MochiChat
  • iPhoneException
  • shell
  • thread
  • memcache

Recent Comments

  • mess assfilled, soderman http://gerero.centerbl...
  • croydon hoes, unlike http://zewero.centerblog.n...
  • Best Site good looking
  • Wonderfull great site
  • very best job
  • felony okcupid, bins http://biiili.centerblog.n...
  • passage mom tube, animosity http://zewero.cente...
  • this is be cool 8)
  • i'm fine good work
  • Good crew it's cool :)

Popular Posts

  • 准备开始学习Erlang了(恶狼, 二郎..)
  • MySQL Innodb备份
  • Lighttpd配置参数
  • iPhone上的HelloWorld终于跑起来了
  • Etag和Expire

Recommended Posts

  • 再次被和谐-_-
  • 十人族: 上线了
  • MooseFS vs. NFS
  • Mochichat新版本上线
  • Gearman
  • MongoDB入门
  • find使用非业余研究
  • MochiChat: 一个基于erlang的web聊天室(beta)
  • /dev/shm和swap的区别
  • gen_tcp的packet参数
  • Beauty - Mai Kuraki
  • awk多维数组
  • We Lost Google.cn
  • Erlang TCP Server例子
  • Mai Kuraki -永远より ながく

Friends' blogs

  • levy
  • sphance
  • andreas

Login

   注册

留言 查看留言

留言

   取消

留言 查看留言


Statistics

  • 访问次数: 88209
  • 今天访问: 5
  • 日志: 187
  • 评论: 516
  • 音乐: 9
  • 用户: 495


 

just DO NOT support IE

close