New to Ruby - pls help in translating this

>Also, as in Awk, "print" with no argument prints the line just read.
>Very simple. If one needs to explicitly refer to the line just read,
>one uses "$_".
>
>And "while gets" is simpler and clearer than "ARGF.each do |elem|".

You would get a lot of support in Perland, but this kind of code is
very out of fashion in the Ruby community.

I can see, for readability's sake, wanting to avoid non-explicit data
handling like using "print" with no argument. Yes, that's the sort of
thing that allows three lines of Perl to do the same work at a dozen or
more of many other languages, but it's not very clear and is easy to
break later when maintaining the script.

I don't see any particular reason to regard "ARGF.each do |elem|" as
better than "while gets", though. The latter of the two is more like
natural language, reads clearly, and is pretty much intuitively
understandable even to people who have never touched the language
before. As I understand it, the "Ruby way" tends toward simplicity,
elegance, and ease of reading, writing, and maintaining. From what I
can see, "while gets" satisfies those preferences better than "ARGF.each
do |elem|". In fact, I'd say that regardless of whether "ARGF.each do

elem>" is what "everyone uses", it is "while gets" that seems to best

suit the spirit of the "Ruby way".

. . . and even if I'm missing some esoteric point regarding how the
language works, and the way "while gets" operates does NOT suit as well
as I think at first glance, perhaps it SHOULD.

Please let me know if I'm missing something. I've got a head full of
snot today, and the sinus pressure might just be making me stupid.

···

On Tue, Dec 13, 2005 at 03:46:09AM +0900, James Edward Gray II wrote:

On Dec 12, 2005, at 12:32 PM, William James wrote:

--
Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]

unix virus: If you're using a unixlike OS, please forward
this to 20 others and erase your system partition.

Chad Perrin wrote:
...

I don't see any particular reason to regard "ARGF.each do |elem|" as
better than "while gets", though. The latter of the two is more like

"ARGF.each do |elem|" has two advantages:

(1) easier to modify to use a different iteration method, such as map or grep.

(2) easier to modify to use a different kind of enumerable, like an array or string, rather than a file or a duck-file like ARGF.

(1) and (2) mean that code written using "ARGF.each do |elem|" can more easily be abstracted/refactored into library code. Of course, if you are writing a one-liner...

Also, I prefer being explicit about the variable elem, rather than using $_, but that's me.

···

--
        vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

`while gets` is also "non-explicit". It makes an assignment you do not see. Compare with:

   ARGF ... each ... |line|

That shows the whole process:

   take the input files ... one line at a time ... and slot them right here

You would be surprised how hard the gets() version can make it to see errors. Here's actual code from a Ruby Quiz submission, that certainly does not work as the author intended (though it may work fine in many cases):

   # ...

   if $0 == __FILE__
     cwstr = nil
     if FileTest.file?( ARGV[0] )
       File.open( ARGV[0] ) { cwstr = gets(nil) }
     end
     $stdout << CrossWord.build( cwstr )
   end

It took me a while to spot the issue. How about you?

Many Rubyists also think something like `line` is prettier than $_.

I hear you though. I came to Ruby through Perl and in the beginning, I used the default variable plenty. I won't be surprised if you search the archives and find me on the other side of this discussion. Now though, I've come to value Ruby's elegance and $_ just looks wrong to me. I guess you can say I'm assimilated.

The moral: $_ works fine (for now!). Use it if you like. Ignore our sneers. :wink:

James Edward Gray II

···

On Dec 12, 2005, at 2:56 PM, Chad Perrin wrote:

On Tue, Dec 13, 2005 at 03:46:09AM +0900, James Edward Gray II wrote:

On Dec 12, 2005, at 12:32 PM, William James wrote:

Also, as in Awk, "print" with no argument prints the line just read.
Very simple. If one needs to explicitly refer to the line just read,
one uses "$_".

And "while gets" is simpler and clearer than "ARGF.each do |elem|".

You would get a lot of support in Perland, but this kind of code is
very out of fashion in the Ruby community.

I can see, for readability's sake, wanting to avoid non-explicit data
handling like using "print" with no argument.

See, I knew I was being stupid about something. Thanks for setting me
straight.

···

On Tue, Dec 13, 2005 at 06:13:19AM +0900, Joel VanderWerf wrote:

(1) easier to modify to use a different iteration method, such as map or
grep.

(2) easier to modify to use a different kind of enumerable, like an
array or string, rather than a file or a duck-file like ARGF.

(1) and (2) mean that code written using "ARGF.each do |elem|" can more
easily be abstracted/refactored into library code. Of course, if you are
writing a one-liner...

--
Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]

unix virus: If you're using a unixlike OS, please forward
this to 20 others and erase your system partition.

I was assuming a distinction between "explicit operation of the
language" and "explicit understanding of the intended functionality of
your code". Some issues with maintainability of something like "wile
gets" have been pointed out to me, though, and I retract my earlier
ramble. Mostly.

···

On Tue, Dec 13, 2005 at 06:26:24AM +0900, James Edward Gray II wrote:

`while gets` is also "non-explicit". It makes an assignment you do
not see. Compare with:

--
Chad Perrin [ CCD CopyWrite | http://ccd.apotheon.org ]

unix virus: If you're using a unixlike OS, please forward
this to 20 others and erase your system partition.