From: Robert Klemme Date: 2009-03-10T17:18:29+09:00 Subject: Re: Setting the contents of a file to a variable? 2009/3/9 Torli Birnbauer : > Robert Klemme wrote: >> tmp$ cat >| x < > All programming languages can be abused. This does not make the abuser a > hero! That was just a tool to create the file. I could have also used a text editor, echo commands or whatever. > PS: whose main message is in the "ps" addendum  must have missed the > point totally! I am sorry, but I believe you are missing the point. The main message was in this line 17:45:25 tmp$ allruby -e 'p File.read("x"), File.readlines("x")' In other words: with methods like File.read and File.readlines which can be as simply used as demonstrated above there is simply no point in using *any* of the presented block forms - especially not File.open("zorba.data", &:readlines) which does not work in pre 1.9 versions - for reading a complete file into a single variable (either as String or as Array). These methods are as safe with regard to file handle closing as the block form of File.open (which I usually advocate when appropriate). *That* is the point that badboy tried to make. Having said that, the simplest and most efficient form to read a number from a file that does not contain much more are probably these - depending on whether a float or an integer is to be read: num = File.read("file1.txt").to_i num = File.read("file1.txt").to_f If file contents are not guaranteed and input verification needs to be done then you can do this as a first line of defense num = Integer(File.read("file1.txt")) num = Float(File.read("file1.txt")) Of course, arbitrary more complex parsing schemes can be devised. Cheers robert -- remember.guy do |as, often| as.you_can - without end