Challenge for the Day - One Liners

J

John Carter

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 : (e-mail address removed)
New Zealand

Refactorers do it a little better every time.
 
E

ES

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
 
B

Bill Kelly

From: "ES 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.

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
 
J

John Carter

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 : (e-mail address removed)
New Zealand

Refactorers do it a little better every time.
 
J

John Carter

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

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

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 : (e-mail address removed)
New Zealand

Refactorers do it a little better every time.
 
E

ES

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 :)
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
 
E

ES

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.


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
 
N

Neil Stevens

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.

Uh huh, and all those attempts add up to way more than a minute. You
save time by writing a little script in a text editor because you can
use your text editor's features to avoid stupid typos. Plus, by having
more than one line, you avoid mistakes from illegibility.

I'd rather not see ruby modified with the intent of supporting poor design
technique. One-liner Ruby should probably be a separate tool based on
ruby. Call it olrb or something.
 
R

Robert Klemme

John Carter said:
find foo -name "*.o" | xargs rm

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

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'

Why not use the globbing interface of Dir directly? You know that you can
do recursive globbing, do you?

12:24:00 [robert.klemme]: ruby -e 'Dir["/etc/*.d/**/*"].each {|f| puts f
if File.file? f}'
/etc/profile.d/00xorg-x11-base.csh
/etc/profile.d/00xorg-x11-base.sh
/etc/profile.d/bindkey.tcsh
/etc/profile.d/complete.tcsh
/etc/profile.d/openssl.csh
/etc/profile.d/openssl.sh
/etc/profile.d/xorg-x11-devel.csh
/etc/profile.d/xorg-x11-devel.sh
/etc/profile.d/xorg-x11-man-pages.csh
/etc/profile.d/xorg-x11-man-pages.sh
/etc/profile.d/zshell.zsh
/etc/rc.d/rc
/etc/rc.d/rc.local
/etc/rc.d/rc.sysinit
/etc/rc.d/init.d/functions
/etc/rc.d/init.d/sshd
12:24:31 [robert.klemme]: ruby -e 'puts Dir["/etc/*.d/**/*"].select {|f|
File.file? f}'
/etc/profile.d/00xorg-x11-base.csh
/etc/profile.d/00xorg-x11-base.sh
/etc/profile.d/bindkey.tcsh
/etc/profile.d/complete.tcsh
/etc/profile.d/openssl.csh
/etc/profile.d/openssl.sh
/etc/profile.d/xorg-x11-devel.csh
/etc/profile.d/xorg-x11-devel.sh
/etc/profile.d/xorg-x11-man-pages.csh
/etc/profile.d/xorg-x11-man-pages.sh
/etc/profile.d/zshell.zsh
/etc/rc.d/rc
/etc/rc.d/rc.local
/etc/rc.d/rc.sysinit
/etc/rc.d/init.d/functions
/etc/rc.d/init.d/sshd

Kind regards

robert
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top