RCR: New interpreter switch

One place where perl’s heavy use of implicit variables scores is in
quick one-liners from the command prompt. I think ruby would be more
appealing in that area if it had a switch that ran the following loop:

while gets
print $_.instance_eval {

}
end

This would generalise the existing technique of mixing a few String
methods like gsub and scan into Kernel and having them act implicitly on
$_.

martin

Its already got that, use ruby -h. -n and -p do those things

Additionally:

$ ri Kernel#gets
----------------------------------------------------------- Kernel::gets
gets( aString=$/ ) → aString or nil

···

Martin DeMello (martindemello@yahoo.com) wrote:

One place where perl’s heavy use of implicit variables scores is in
quick one-liners from the command prompt. I think ruby would be more
appealing in that area if it had a switch that ran the following loop:

while gets
print $_.instance_eval {

}
end

This would generalise the existing technique of mixing a few String
methods like gsub and scan into Kernel and having them act implicitly on
$_.


 Returns (and assigns to $_) the next line from the list of files in
 ARGV (or $*), or from standard input if no files are present on the
 command line. Returns nil at end of file. The optional argument
 specifies the record separator. The separator is included with the
 contents of each record. A separator of nil reads the entire
 contents, and a zero-length separator reads the input one paragraph
 at a time, where paragraphs are divided by two consecutive
 newlines. If multiple filenames are present in ARGV, gets(nil) will
 read the contents one file at a time.
    ARGV << "testfile"
    print while gets
 produces:
    This is line one
    This is line two
    This is line three
    And so on...

$ ri Kernel#scan
----------------------------------------------------------- Kernel::scan
scan( pattern ) → anArray
scan( pattern ) {| | block } → $_

 Equivalent to calling $_.scan. See String#scan on page 378.

$ ri Kernel#gsub
----------------------------------------------------------- Kernel::gsub
gsub( pattern, replacement ) → aString
gsub( pattern ) {| | block } → aString

 Equivalent to $_.gsub..., except that $_ receives the modified
 result.
    $_ = "quick brown fox"
    gsub /[aeiou]/, '*'   #=> "q**ck br*wn f*x"
    $_                    #=> "q**ck br*wn f*x"


Eric Hodel - drbrain@segment7.net - http://segment7.net
All messages signed with fingerprint:
FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04

An alternative would be to create a file that adds all the methods you
want to Kernel, and require that on the command line. Just a thought.

Gavin

···

On Sunday, September 21, 2003, 11:46:33 PM, Martin wrote:

One place where perl’s heavy use of implicit variables scores is in
quick one-liners from the command prompt. I think ruby would be more
appealing in that area if it had a switch that ran the following loop:

while gets
print $_.instance_eval {

}
end

This would generalise the existing technique of mixing a few String
methods like gsub and scan into Kernel and having them act implicitly on
$_.

while gets
print $_.instance_eval {

}
end

This would generalise the existing technique of mixing a few String
methods like gsub and scan into Kernel and having them act implicitly on
$_.

Its already got that, use ruby -h. -n and -p do those things

No they don’t. For instance, calling the new flag -j (all the good names
were taken!)

ruby -n -e ‘puts $.split.join(" | ")’
ruby -p -e '$
= $_.split.join(" | ") + “\n”’
ruby -j -e ‘split.join(" | ") + “\n”’

(and a nice convenience method to go along would be String#n, to add a
newline to the end of a string, so we could say ‘split.join.n’)

Additionally:

$ ri Kernel#gets

[…]

Yes, that was the ‘existing technique’ I referred to.

martin

···

Eric Hodel drbrain@segment7.net wrote:

Martin DeMello (martindemello@yahoo.com) wrote:

I thought of that - it doesn’t work because in several cases you’re
still left with having to assign the result to $_ or call print/puts
explicitly. A method that will work is to wrap a shell script around
ruby, that puts the loop and the instance_eval in for you. But it’d be
nicer to see it as a switch.

martin

···

Gavin Sinclair gsinclair@soyabean.com.au wrote:

On Sunday, September 21, 2003, 11:46:33 PM, Martin wrote:

One place where perl’s heavy use of implicit variables scores is in
quick one-liners from the command prompt. I think ruby would be more
appealing in that area if it had a switch that ran the following loop:

while gets
print $_.instance_eval {

}
end

This would generalise the existing technique of mixing a few String
methods like gsub and scan into Kernel and having them act implicitly on
$_.

An alternative would be to create a file that adds all the methods you
want to Kernel, and require that on the command line. Just a thought.