RCR: New interpreter switch

M

Martin DeMello

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
 
M

Martin DeMello

Eric Hodel said:
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
 
G

Gavin Sinclair

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.

Gavin
 
M

Martin DeMello

Gavin Sinclair said:
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.

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top