Challenge for the Day - One Liners

Perl has justly been called the "Swiss Army Chain Saw" of Unix System Administration.

Much of this reputation has centered on the amazing things you can do in just one line of code from the command line.

Ruby's more readable and cleaner syntax can, on occasions, get in the way, make our one liners a few characters longer, and on other occasions Rubies objects all the way down power makes our one liners several characters shorter.

Challenge for the Day :-
   Think Laterally on how to tweak Ruby to be better at One Liners.

Please put your responses under the following "Hats"...(http://www.mycoted.com/creativity/techniques/sixhats.php)

(Tip: Save your "It (won't work / we don't need)" notions for the Black Hat slot)

White Hat - Objective Thinking - What techniques and resources do we already have in Ruby for One Liners?

···

====================================

Command Line options
-e 'Some ruby'
-n 'Iterate over stdin'
-i.bak Backup file
-l Chomp!
-rmodule
-p print each line
-a autosplint
-Fpattern
-Orecord seperator

Idioms
ruby -nple 'gsub(regex,replacement)'
ruby -i.bak -nple 'expr' files.*
ruby -nle 'puts "stuff #{expr involving $1,$2,...}" if /blah(bloo)/'
Histogram...
ruby -nle 'BEGIN{$a=Hash.new(0)};$a[$1]+=1 if /(PATTERN)/;END{$a.each_pair{|k,n| puts "#{n}\t#{k}"}}'

Modules
eg. -rcsv does juicy things.

Your standard techniques and Idioms here please...

Red Hat Thinking - Emotions

eg. The BEGIN/END stuff and the -a stuff always feels slightly clunky to me.

Your emotions here.

Yellow Hat thinking - Glowingly positive.

Think of what could be if magic happens. List the wild "Wouldn't it be nice..."

eg. Wouldn't it be nice if rexml had some oneliner specific shortcuts?

Green Hat thinking - Creative hunt for alternatives, provocations, changes =================================================================

What new options could we introduce. eg. -B and a -E option that work like -e, but instead of the clunky BEGIN, END blocks.

What modules to -r? Perhaps -n should implicitly -r some modules?

Can we command line option an idiom?

Can we modularize an Idiom?

Can we...? Your wild questions and Ideas here.

Black Hat thinking:

The hat of judgment and caution.

Blue Hat Thinking:

This email is starting a process, how can we deploy the results? eg. RubyGarden Wiki, rubyforge "Code Snuppets", fold into Ruby CVS mainline, ... Your ideas here.

John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : john.carter@tait.co.nz
New Zealand

Refactorers do it a little better every time.

Perl has justly been called the "Swiss Army Chain Saw" of Unix System
Administration.

Much of this reputation has centered on the amazing things you can do
in just one line of code from the command line.

Ruby's more readable and cleaner syntax can, on occasions, get in the way,
make our one liners a few characters longer, and on other occasions
Rubies objects all the way down power makes our one liners several
characters shorter.

I'd rather not, thanks. It takes about one minute to start up vi and save
the file afterwards. You can also use it again tomorrow.

This was interesting to me, though: "Can we command line option an idiom?".
Can we, instead, idiom the CLI?

Challenge for the Day :-
   Think Laterally on how to tweak Ruby to be better at One Liners.

Please put your responses under the following
"Hats"...(http://www.mycoted.com/creativity/techniques/sixhats.php\)

(Tip: Save your "It (won't work / we don't need)" notions for the Black
Hat slot)

White Hat - Objective Thinking - What techniques and resources do we
already have in Ruby for One Liners?

Command Line options
-e 'Some ruby'
-n 'Iterate over stdin'
-i.bak Backup file
-l Chomp!
-rmodule
-p print each line
-a autosplint
-Fpattern
-Orecord seperator

Idioms
ruby -nple 'gsub(regex,replacement)'
ruby -i.bak -nple 'expr' files.*
ruby -nle 'puts "stuff #{expr involving $1,$2,...}" if /blah(bloo)/'
Histogram...
ruby -nle 'BEGIN{$a=Hash.new(0)};$a[$1]+=1 if
/(PATTERN)/;END{$a.each_pair{|k,n| puts "#{n}\t#{k}"}}'

Modules
eg. -rcsv does juicy things.

Your standard techniques and Idioms here please...

Red Hat Thinking - Emotions

eg. The BEGIN/END stuff and the -a stuff always feels slightly clunky to
me.

Your emotions here.

Yellow Hat thinking - Glowingly positive.

Think of what could be if magic happens. List the wild "Wouldn't it be
nice..."

eg. Wouldn't it be nice if rexml had some oneliner specific shortcuts?

Green Hat thinking - Creative hunt for alternatives, provocations, changes

What new options could we introduce. eg. -B and a -E option that
work like -e, but instead of the clunky BEGIN, END blocks.

What modules to -r? Perhaps -n should implicitly -r some modules?

Can we command line option an idiom?

Can we modularize an Idiom?

Can we...? Your wild questions and Ideas here.

Black Hat thinking:

The hat of judgment and caution.

Blue Hat Thinking:

This email is starting a process, how can we deploy the results? eg.
RubyGarden Wiki, rubyforge "Code Snuppets", fold into Ruby CVS mainline,
... Your ideas here.

John Carter

E

···

On Mon, March 7, 2005 10:15 pm, John Carter said:

> Ruby's more readable and cleaner syntax can, on occasions, get in the way,
> make our one liners a few characters longer, and on other occasions
> Rubies objects all the way down power makes our one liners several
> characters shorter.

I'd rather not, thanks. It takes about one minute to start up vi and save
the file afterwards. You can also use it again tomorrow.

Just curious, do you make a distinction between ruby and other
command line tools in this regard? Or do you put everything
you type at the command line in a file first?

Like...

find foo -name "*.o" | xargs rm

grep sshd /var/log/auth.log | grep password

ls -lt */*.xyzzy | head -n1

find bar -type d -name CVS -exec rm -r {} \;

etc.

Myself, unless I find I'm typing the same command over
and over, I tend to just type whatever I need right at
the command line, right at whatever directory I'm in when
I need it, and move on.

So for me, just like the non-ruby commands above, I'd be
similarly disinclined to type things like the following
into an editor:

ruby -e 'p ARGF.read.scan(/plover/i).length' *.baz

ruby -i~ -pe 'gsub(/wave rod/,"drop rod")' *.adv

So I was wondering if you would have typed all of the
above into an editor? Or only the ruby ones? And if
only the ruby ones, why ruby is different?

Regards,

Bill

···

From: "ES" <ruby-ml@magical-cat.org>

I'd rather not, thanks. It takes about one minute to start up vi and save
the file afterwards. You can also use it again tomorrow.

Ah, but one minute is far too long. You see One Liners are developed iteratively and incrementally on the command line.

Each attempt piping in the data from STDIN, letting the output go to STDOUT for instant display.

You have the editing power of GNU ReadLine and up arrow, and simply Enter to execute.

Instantaneous feedback.

Tiny steps incremental development. You never just stuck saying "Why didn't that massive regex work?"

When it is working you pipe the output to the destination file.

Then you stash the oneliner for future reference ever with
   echo 'one liner' > file
or cut and paste into vi/emacs/...

I bind Emacs M-x compile to F12, and often develope one liners there.

This was interesting to me, though: "Can we command line option an idiom?".
Can we, instead, idiom the CLI?

Ah. Theres a thought. We need the notion of Pipes and filters in ruby shell.

John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : john.carter@tait.co.nz
New Zealand

Refactorers do it a little better every time.

···

On Tue, 8 Mar 2005, ES wrote:

Hmm, thats another good Idiom. I often merge that in with backtick or
   open("|find foo -name '*.o'|filter1|filter2")

There is another Red Hat feeling for you. The Find module is ideal for Oneliners, but somehow Find.find( path, path, ...) is too verbose. It needs a globbing interface like Dir. eg.

class Find
   def Find.(string)
     Find.find(*Dir[string]) do |path|
       yield path
     end
   end
end

Tie that to a command line option would be sweet
   eg. ruby --find='/etc/*.d' --type=f -e 'Do stuff per file coming by'

John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : john.carter@tait.co.nz
New Zealand

Refactorers do it a little better every time.

···

On Tue, 8 Mar 2005, Bill Kelly wrote:

find foo -name "*.o" | xargs rm

grep sshd /var/log/auth.log | grep password

From: "ES" <ruby-ml@magical-cat.org>

> Ruby's more readable and cleaner syntax can, on occasions, get in the way,
> make our one liners a few characters longer, and on other occasions
> Rubies objects all the way down power makes our one liners several
> characters shorter.

I'd rather not, thanks. It takes about one minute to start up vi and save
the file afterwards. You can also use it again tomorrow.

Just curious, do you make a distinction between ruby and other
command line tools in this regard? Or do you put everything
you type at the command line in a file first?

Like...

find foo -name "*.o" | xargs rm

grep sshd /var/log/auth.log | grep password

ls -lt */*.xyzzy | head -n1

find bar -type d -name CVS -exec rm -r {} \;

etc.

You're invoking programs, not code :slight_smile:

Myself, unless I find I'm typing the same command over
and over, I tend to just type whatever I need right at
the command line, right at whatever directory I'm in when
I need it, and move on.

So for me, just like the non-ruby commands above, I'd be
similarly disinclined to type things like the following
into an editor:

ruby -e 'p ARGF.read.scan(/plover/i).length' *.baz

ruby -i~ -pe 'gsub(/wave rod/,"drop rod")' *.adv

So I was wondering if you would have typed all of the
above into an editor? Or only the ruby ones? And if
only the ruby ones, why ruby is different?

No, but let me clarify one thing: I am, in particular,
(tentatively) opposed to adding any 'shortcuts' to the
language syntax (not that the decision is up to me, anyway).
Maybe we need a sed/awk replacement program in Ruby, though?

Regards,

Bill

E

···

On Tue, March 8, 2005 1:48 am, Bill Kelly said:

I'd rather not, thanks. It takes about one minute to start up vi and save
the file afterwards. You can also use it again tomorrow.

Ah, but one minute is far too long. You see One Liners are developed
iteratively and incrementally on the command line.

Each attempt piping in the data from STDIN, letting the output go to
STDOUT for instant display.

You have the editing power of GNU ReadLine and up arrow, and simply Enter
to execute.

Instantaneous feedback.

Tiny steps incremental development. You never just stuck saying "Why
didn't that massive regex work?"

When it is working you pipe the output to the destination file.

I don't know.. I tend to think that if one needs to think about it,
it's probably not a one-liner (like, say, 'find / -iname '???.c*'').

Then you stash the oneliner for future reference ever with
   echo 'one liner' > file
or cut and paste into vi/emacs/...

I bind Emacs M-x compile to F12, and often develope one liners there.

This was interesting to me, though: "Can we command line option an idiom?".
Can we, instead, idiom the CLI?

Ah. Theres a thought. We need the notion of Pipes and filters in ruby
shell.

I think an actual Ruby shell (wrt. bash/csh/etc.) has been discussed before.
I wouldn't discount that idea.

John Carter

E

···

On Tue, March 8, 2005 2:20 am, John Carter said:

On Tue, 8 Mar 2005, ES wrote: