translate Perl diamond operator to Ruby

Discussion in 'Ruby' started by Chad Perrin, Apr 15, 2007.

  1. Chad Perrin

    Chad Perrin Guest

    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.apotheon.org ]
    "The ability to quote is a serviceable
    substitute for wit." - W. Somerset Maugham
    Chad Perrin, Apr 15, 2007
    #1
    1. Advertising

  2. Chad Perrin

    Ken Bloom Guest

    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/~kbloom1/
    Ken Bloom, Apr 15, 2007
    #2
    1. Advertising

  3. 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.
    Brian Candler, Apr 15, 2007
    #3
  4. 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.
    Benjohn Barnes, Apr 15, 2007
    #4
  5. Chad Perrin

    Chad Perrin Guest

    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/?m=200701 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.apotheon.org ]
    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, Apr 15, 2007
    #5
  6. Chad Perrin

    Chad Perrin Guest

    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.apotheon.org ]
    "A script is what you give the actors. A program
    is what you give the audience." - Larry Wall
    Chad Perrin, Apr 15, 2007
    #6
  7. Chad Perrin

    Chad Perrin Guest

    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.apotheon.org ]
    print substr("Just another Perl hacker", 0, -2);
    Chad Perrin, Apr 15, 2007
    #7
  8. 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 Edward Gray II, Apr 16, 2007
    #8
  9. 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
    >>> =3D> Object

    >>
    >> ... an Object.
    >>

    >
    > Now that I know ARGF exists . . .
    >
    > http://www.danvk.org/wp/?m=3D200701 reports the following:
    >
    > ARGF is a great crutch for Perl programmers who miss typing while=20
    > (<>)
    > {...}. It opens each input file left in ARGV and yields each =20
    > line. If
    > there=B4s 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 =20
    implementation of some very common behavior for command-line =20
    programs. I don't feel it exists merely as a crutch for Perl =20
    programmers.

    James Edward Gray II=
    James Edward Gray II, Apr 16, 2007
    #9
  10. Chad Perrin

    Chad Perrin Guest

    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/?m=200701 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.apotheon.org ]
    "It's just incredible that a trillion-synapse computer could actually
    spend Saturday afternoon watching a football game." - Marvin Minsky
    Chad Perrin, Apr 16, 2007
    #10
  11. 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
    Robert Klemme, Apr 16, 2007
    #11
  12. Chad Perrin

    Chad Perrin Guest

    On Mon, Apr 16, 2007 at 08:05:05PM +0900, Robert Klemme wrote:
    > 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


    That's useful, and very similar to Perl, but in practice I find that I
    don't take that approach very often at all. The only command/shebang
    line switch I use with any frequency in Perl is -l, for adding a newline
    to the end of all print statements, and since Ruby has puts I don't need
    to do that in Ruby. Usually, the programs I find myself writing don't
    neatly fit into a while loop -- except when writing a toy script to
    demonstrate something, in which case it usually pays to be explicit with
    the while loop anyway -- so the while (<>) { . . . } idiom in Perl (and
    now the ARGF.each do |foo| . . . end idiom in Ruby) is far more useful
    to me.

    --
    CCD CopyWrite Chad Perrin [ 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, Apr 16, 2007
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jay Dean
    Replies:
    57
    Views:
    26,176
    Arne Vajhøj
    Dec 10, 2013
  2. Juha Nieminen
    Replies:
    1
    Views:
    341
    James Kanze
    Mar 2, 2009
  3. J. Romano

    binmode and the diamond operator

    J. Romano, Nov 8, 2003, in forum: Perl Misc
    Replies:
    6
    Views:
    182
    J. Romano
    Nov 17, 2003
  4. maryellen sniffen

    <> the Diamond Operator

    maryellen sniffen, Dec 31, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    119
    Anno Siegel
    Dec 31, 2004
  5. Roedy Green

    diamond operator

    Roedy Green, Apr 4, 2012, in forum: Java
    Replies:
    25
    Views:
    1,140
    Arne Vajhøj
    May 6, 2012
Loading...

Share This Page