From: Robert Klemme Date: 2009-03-10T01:46:39+09:00 Subject: Re: Setting the contents of a file to a variable? 2009/3/9 Torli Birnbauer : > badboy wrote: >> did you read my post? why should we use a construct with a block if >> there are several methods to do this directly? > > Because the block way is the Ruby way, besides it's much more efficient > it does open, read and close in one short command! Beside it its > elegance it also has a pedagogical value, namely, it highlights and > exposes the use of Ruby 1.9 usage of Symbol class to_proc strategy. So > all I can say is: Matthias, excellent contribution to this thread, very > good job. Frankly, I believe you haven't properly read badboy's posting. I'll show his code below. > Perhaps only a comment that even reading multi-line file into a variable > sometimes can be useful with: > >  guess = File.open("zorba.data", &:readlines) > > Clearly the old way in comparison to the the block is more like the > spaghetti code! Like these? 17:45:12 tmp$ cat >| x < 123 > 456 > EOF 17:45:23 tmp$ cat x 123 456 17:45:25 tmp$ allruby -e 'p File.read("x"), File.readlines("x")' ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin] "123\n456\n" ["123\n", "456\n"] ruby 1.9.1p0 (2009-01-30 revision 21907) [i386-cygwin] "123\n456\n" ["123\n", "456\n"] 17:45:55 tmp$ Very spaghetti indeed. Cheers robert PS: This posting contains traces of irony. Please handle with care. -- remember.guy do |as, often| as.you_can - without end