[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

translate Perl diamond operator to Ruby

Chad Perrin

4/15/2007 6:07:00 PM

Over the years, I've found the following to be an excellent way to whip
up quick, very useful Perl scripts:

#!/usr/bin/perl

while(<>) {
# do stuff . . .
}

Is there an equivalent idiom in Ruby? So far as I'm aware, there's no
catch-all diamond operator in Ruby that allows one to create a default
input behavior for a script that accepts either a filename or piped
output of another command the way this works in Perl. I certainly hope
there's an equivalent, though.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
"The ability to quote is a serviceable
substitute for wit." - W. Somerset Maugham

11 Answers

Ken Bloom

4/15/2007 6:38:00 PM

0

On Mon, 16 Apr 2007 03:06:58 +0900, Chad Perrin wrote:

> Over the years, I've found the following to be an excellent way to whip
> up quick, very useful Perl scripts:
>
> #!/usr/bin/perl
>
> while(<>) {
> # do stuff . . .
> }
>
> Is there an equivalent idiom in Ruby? So far as I'm aware, there's no
> catch-all diamond operator in Ruby that allows one to create a default
> input behavior for a script that accepts either a filename or piped
> output of another command the way this works in Perl. I certainly hope
> there's an equivalent, though.

ARGF.each do |line|
# do stuff
end

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu...

Brian Candler

4/15/2007 8:26:00 PM

0

On Mon, Apr 16, 2007 at 03:06:58AM +0900, Chad Perrin wrote:
> Over the years, I've found the following to be an excellent way to whip
> up quick, very useful Perl scripts:
>
> #!/usr/bin/perl
>
> while(<>) {
> # do stuff . . .
> }
>
> Is there an equivalent idiom in Ruby? So far as I'm aware, there's no
> catch-all diamond operator in Ruby that allows one to create a default
> input behavior for a script that accepts either a filename or piped
> output of another command the way this works in Perl. I certainly hope
> there's an equivalent, though.

while gets
# do stuff with $_
end

Or, less perly,

while line = gets
# do stuff with line
end

Or, if there are multiple files listed on ARGV, and you want to slurp them
all one at a time,

while contents = gets(nil)
# do stuff with contents
end

Doing that in Perl seems quite hard.

B.

benjohn

4/15/2007 8:27:00 PM

0


On 15 Apr 2007, at 19:40, Ken Bloom wrote:

> On Mon, 16 Apr 2007 03:06:58 +0900, Chad Perrin wrote:
>
>> Over the years, I've found the following to be an excellent way to
>> whip
>> up quick, very useful Perl scripts:
>>
>> #!/usr/bin/perl
>>
>> while(<>) {
>> # do stuff . . .
>> }
>>
>> Is there an equivalent idiom in Ruby? So far as I'm aware,
>> there's no
>> catch-all diamond operator in Ruby that allows one to create a
>> default
>> input behavior for a script that accepts either a filename or piped
>> output of another command the way this works in Perl. I certainly
>> hope
>> there's an equivalent, though.
>
> ARGF.each do |line|
> # do stuff
> end

What _is_ ARGF? I've printed it's class, but apparently it is:

> irb(main):003:0> ARGF.class
> => Object

... an Object.

Chad Perrin

4/15/2007 10:08:00 PM

0

On Mon, Apr 16, 2007 at 05:27:24AM +0900, Benjohn Barnes wrote:
>
> What _is_ ARGF? I've printed it's class, but apparently it is:
>
> >irb(main):003:0> ARGF.class
> >=> Object
>
> ... an Object.
>

Now that I know ARGF exists . . .

http://www.danvk.org/wp... reports the following:

ARGF is a great crutch for Perl programmers who miss typing while(<>)
{...}. It opens each input file left in ARGV and yields each line. If
there´s no input files left, it reads STDIN and yields each line it
gets there. Many, many programs do their work in an ARGF loop.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
McCloctnick the Lucid: "The first rule of magic is simple. Don't waste
your time waving your hands and hopping when a rock or a club will do."

Chad Perrin

4/15/2007 10:09:00 PM

0

On Mon, Apr 16, 2007 at 03:40:17AM +0900, Ken Bloom wrote:
> On Mon, 16 Apr 2007 03:06:58 +0900, Chad Perrin wrote:
>
> > Over the years, I've found the following to be an excellent way to whip
> > up quick, very useful Perl scripts:
> >
> > #!/usr/bin/perl
> >
> > while(<>) {
> > # do stuff . . .
> > }
> >
> > Is there an equivalent idiom in Ruby? So far as I'm aware, there's no
> > catch-all diamond operator in Ruby that allows one to create a default
> > input behavior for a script that accepts either a filename or piped
> > output of another command the way this works in Perl. I certainly hope
> > there's an equivalent, though.
>
> ARGF.each do |line|
> # do stuff
> end

Thanks muchly. Armed with this seed of knowledge (to mix a metaphor), I
can find the rest via Google easily. I appreciate it.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
"A script is what you give the actors. A program
is what you give the audience." - Larry Wall

Chad Perrin

4/15/2007 10:23:00 PM

0

On Mon, Apr 16, 2007 at 05:25:41AM +0900, Brian Candler wrote:
>
> while contents = gets(nil)
> # do stuff with contents
> end
>
> Doing that in Perl seems quite hard.

Thanks for the information. As for this last example, the functionality
you achieved isn't exactly "hard" in Perl, but it's a touch less
intuitive. Slurping a file via the diamond operator should end up
looking something like this, generally:

my $foo;
{ local $/; $foo = <>; }

The "my $foo" part, for those not familiar with Perl, is just the way
the $foo variable can be declared with lexical scope. If you're writing
code without strict and warnings pragmas (indispensable debugging aids
in Perl), you could dispense with the "my $foo" line altogether.

In fact, you could dispense with the braces and use "undef $/" instead
of "local $/" if you prefer, as long as you don't care about getting the
original value of $/ back (or want to put it back in manually for some
reason).

TIMTOWTDI.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
print substr("Just another Perl hacker", 0, -2);

James Gray

4/16/2007 3:15:00 AM

0

On Apr 15, 2007, at 3:25 PM, Brian Candler wrote:

> Or, less perly,
>
> while line = gets
> # do stuff with line
> end

I really feel the Ruby version is to use a standard iterator:

ARGF.each do |line|
# do stuff with line
end

> Or, if there are multiple files listed on ARGV, and you want to
> slurp them
> all one at a time,
>
> while contents = gets(nil)
> # do stuff with contents
> end

contents = ARGF.read

James Edward Gray II

James Gray

4/16/2007 3:18:00 AM

0

On Apr 15, 2007, at 5:07 PM, Chad Perrin wrote:

> On Mon, Apr 16, 2007 at 05:27:24AM +0900, Benjohn Barnes wrote:
>>
>> What _is_ ARGF? I've printed it's class, but apparently it is:
>>
>>> irb(main):003:0> ARGF.class
>>> => Object
>>
>> ... an Object.
>>
>
> Now that I know ARGF exists . . .
>
> http://www.danvk.org/wp... reports the following:
>
> ARGF is a great crutch for Perl programmers who miss typing while
> (<>)
> {...}. It opens each input file left in ARGV and yields each
> line. If
> there´s no input files left, it reads STDIN and yields each line it
> gets there. Many, many programs do their work in an ARGF loop.

I don't much care for that description. ARGF simplifies the
implementation of some very common behavior for command-line
programs. I don't feel it exists merely as a crutch for Perl
programmers.

James Edward Gray II

Chad Perrin

4/16/2007 4:17:00 AM

0

On Mon, Apr 16, 2007 at 12:18:12PM +0900, James Edward Gray II wrote:
> On Apr 15, 2007, at 5:07 PM, Chad Perrin wrote:
>
> >On Mon, Apr 16, 2007 at 05:27:24AM +0900, Benjohn Barnes wrote:
> >>
> >>What _is_ ARGF? I've printed it's class, but apparently it is:
> >>
> >>>irb(main):003:0> ARGF.class
> >>>=> Object
> >>
> >>... an Object.
> >>
> >
> >Now that I know ARGF exists . . .
> >
> >http://www.danvk.org/wp... reports the following:
> >
> > ARGF is a great crutch for Perl programmers who miss typing while
> >(<>)
> > {...}. It opens each input file left in ARGV and yields each
> >line. If
> > there?s no input files left, it reads STDIN and yields each line it
> > gets there. Many, many programs do their work in an ARGF loop.
>
> I don't much care for that description. ARGF simplifies the
> implementation of some very common behavior for command-line
> programs. I don't feel it exists merely as a crutch for Perl
> programmers.

Nor do I. I found the more implementation-related description to be
somewhat useful, however, once I got past the opining.

--
CCD CopyWrite Chad Perrin [ http://ccd.ap... ]
"It's just incredible that a trillion-synapse computer could actually
spend Saturday afternoon watching a football game." - Marvin Minsky

Robert Klemme

4/16/2007 11:05:00 AM

0

On 16.04.2007 00:08, Chad Perrin wrote:
> On Mon, Apr 16, 2007 at 03:40:17AM +0900, Ken Bloom wrote:
>> On Mon, 16 Apr 2007 03:06:58 +0900, Chad Perrin wrote:
>>
>>> Over the years, I've found the following to be an excellent way to whip
>>> up quick, very useful Perl scripts:
>>>
>>> #!/usr/bin/perl
>>>
>>> while(<>) {
>>> # do stuff . . .
>>> }
>>>
>>> Is there an equivalent idiom in Ruby? So far as I'm aware, there's no
>>> catch-all diamond operator in Ruby that allows one to create a default
>>> input behavior for a script that accepts either a filename or piped
>>> output of another command the way this works in Perl. I certainly hope
>>> there's an equivalent, though.
>> ARGF.each do |line|
>> # do stuff
>> end
>
> Thanks muchly. Armed with this seed of knowledge (to mix a metaphor), I
> can find the rest via Google easily. I appreciate it.

You can also use one of the command line switches -n or -p, e.g.

13:04:42 [tmp]: ls -l | ruby -ne 'puts "<#{$_[0,3]}>"'
<tot>
<-rw>
<lrw>
13:04:59 [tmp]:

Kind regards

robert