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.
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!
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!
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
···
2009/3/9 Torli Birnbauer <aipir@sympatico.ca>:
--
remember.guy do |as, often| as.you_can - without end
Oh, I do not think I am the one missing anything. I challenge you to
first explain your sarcastic concoction "tmp$ cat >| x <<EOF" which I
claim is an exhibitionist's exaggeration of "apparent" Unix/Perl - Ruby
like gibberish. The point I was making was that a succinct Ruby1.9's way
of using a Symbol class to_proc strategy, as Matthias showed us, is
superior to anything else discussed here. You two, Robert and especially
badboy, complained about using a block when a simpler, and by most from
M$ emerging Ruby programmers better understood, spaghetti code exists.
If anybody should take seriously what you, Robert and badboy, are
saying regular expressions, and indeed Unix shell and all its cryptic,
and powerful paraphernalia are pure garbage. And since today's mouse
pushing computer "professionals" do not see Ruby until they loose their
"secure" jobs in M$ dominated environments, it looks that such opinions
as yours are a credible ones. The truth is, that those who write
gibberish like: "tmp$ cat >| x <<EOF" are appealing to the ignorant
public who can not discern a fake from a true argument, and most likely
have not had a chance to rise above that ignorance themselves.
Now this argument has turned into pseudo serious technical discussion,
which is only obfuscating the real issue here and indeed is missing a
point.
I challenge you to first explain your sarcastic concoction "tmp$ cat >| x <<EOF" which I claim is an exhibitionist's exaggeration of "apparent" Unix/Perl - Ruby like gibberish.
It's actually a pretty typical command line, and to Robert's credit, very concise.
tmp$ is a shell prompt meaning, by convention, that the shell is a Bourne derivative, and that its current working directory is called tmp.
cat copies its standard input to its standard output.
>> means tells the shell to redirect cat's output to a file, and to overwrite the file if it already exists.
x is the name of the output file.
<<EOF means tells the shell to keep reading input for cat, until a line containing just the letters EOF is encountered. EOF is a conventional abbreviation for "end of file."
This is one of those rare moments where RTFM really does apply.
This is one of those rare moments where RTFM really does apply.
Yet another defender of obfuscation and deception with fabricated
gibberish, only to glorify some minuscule know-how with RTFM phrase.
The code Robert wrote is Unix shell code and has nothing to do with Ruby
discussion here. Besides, it should be mentioned that it is shell and on
top of that if written properly for a discussion like this should look
like the following:
The intention of the above clearly is to show a sarcastic opinion of a
frustrated mouse pusher, who has to bury his point in garbage to confuse
the audience. I asked him to explain what he wrote and the answer to the
above gibberish is very simple: