Piping into ruby at command line

How do I make something like this work ?

% cat junk.txt | ruby -e “puts $_”

I cannot seem to make ruby accept std input when it is in a “pipe” mode at
command line.

I am using cygwin Ruby 1.6.7

TIA,

– Shanko

$ cat >junk.txt <<EOT
asdf
1234
EOT

$ cat junk.txt | ruby -ne “print $_”

Strangely, this didn’t do what I expected:

$ cat junk.txt | ruby -ne “puts $_”

Using #puts, I got two blank lines. This happens in both 1.6.7 and
1.7.2:

ruby 1.6.7 (2002-03-19) [i386-linux]
ruby 1.7.2 (2002-05-30) [i686-linux]

– Dossy

···

On 2002.07.23, Shashank Date ADATE@kc.rr.com wrote:

How do I make something like this work ?

% cat junk.txt | ruby -e “puts $_”

I cannot seem to make ruby accept std input when it is in a “pipe” mode at
command line.

I am using cygwin Ruby 1.6.7


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

Shashank Date wrote:

How do I make something like this work ?

% cat junk.txt | ruby -e “puts $_”

I cannot seem to make ruby accept std input when it is in a “pipe” mode at
command line.

cat file | ruby -n -e ‘puts $_’

Some related switches:

-e ‘command’ one line of script. Several -e’s allowed.
-n assume ‘while gets(); … end’ loop around your script
-p assume loop like -n but print line also like sed
-a autosplit mode with -n or -p (splits $_ into $F)
-Fpattern split() pattern for autosplit (-a)

Good luck!

···


Mike Hall
http://www.enteract.com/~mghall

Hi –

$ cat >junk.txt <<EOT
asdf
1234
EOT

$ cat junk.txt | ruby -ne “print $_”

Strangely, this didn’t do what I expected:

$ cat junk.txt | ruby -ne “puts $_”

Using #puts, I got two blank lines. This happens in both 1.6.7 and
1.7.2:

ruby 1.6.7 (2002-03-19) [i386-linux]
ruby 1.7.2 (2002-05-30) [i686-linux]

OK, after some mystification, I figured out what’s happening.

The shell is interpolating its own $_ – which is nothing.

So what you’re really getting is just “print” and “puts”.

print uses $_ (the Ruby one) by default. puts doesn’t.

Try putting the inline script in single quotes instead of double, or
escape the $ with a \ .

David

···

On Tue, 23 Jul 2002, Dossy wrote:


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Right. I figured that out right after I wrote the email as
I was smoking my cigarette before my 2:00 PM meeting. I would
have replied to my own email if I weren’t sitting in the
meeting …

I normally always use single quotes when typing code – this time
I used double quotes because I was cutting and pasting what the
OP wrote.

Defeated by my own laziness … ouch.

– Dossy

···

On 2002.07.23, David Alan Black dblack@candle.superlink.net wrote:

OK, after some mystification, I figured out what’s happening.

The shell is interpolating its own $_ – which is nothing.

So what you’re really getting is just “print” and “puts”.

print uses $_ (the Ruby one) by default. puts doesn’t.

Try putting the inline script in single quotes instead of double, or
escape the $ with a \ .


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

Thanks a lot, David ! You nailed it …

Thanks to you too Dossy and Mike for the suggestions.

“David Alan Black” dblack@candle.superlink.net wrote in message
news:Pine.LNX.4.30.0207221426320.1660-100000@candle.superlink.net

···

Hi –

On Tue, 23 Jul 2002, Dossy wrote:

$ cat >junk.txt <<EOT
asdf
1234
EOT

$ cat junk.txt | ruby -ne “print $_”

Strangely, this didn’t do what I expected:

$ cat junk.txt | ruby -ne “puts $_”

Using #puts, I got two blank lines. This happens in both 1.6.7 and
1.7.2:

ruby 1.6.7 (2002-03-19) [i386-linux]
ruby 1.7.2 (2002-05-30) [i686-linux]

OK, after some mystification, I figured out what’s happening.

The shell is interpolating its own $_ – which is nothing.

So what you’re really getting is just “print” and “puts”.

print uses $_ (the Ruby one) by default. puts doesn’t.

Try putting the inline script in single quotes instead of double, or
escape the $ with a \ .

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav