[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Challenge for the Day - One Liners

John Carter

3/7/2005 10:15:00 PM

9 Answers

ES

3/8/2005 12:58:00 AM

0

On Mon, March 7, 2005 10:15 pm, John Carter said:
> 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/s...)
>
> (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




Bill Kelly

3/8/2005 1:49:00 AM

0

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.

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




John Carter

3/8/2005 2:20:00 AM

0

John Carter

3/8/2005 2:28:00 AM

0

ES

3/8/2005 3:25:00 AM

0

On Tue, March 8, 2005 1:48 am, Bill Kelly said:
> 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 :)

> 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




ES

3/8/2005 3:30:00 AM

0

On Tue, March 8, 2005 2:20 am, John Carter said:
> On Tue, 8 Mar 2005, ES wrote:
>
>> 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



Martin DeMello

3/8/2005 4:38:00 AM

0

John Carter <john.carter@tait.co.nz> wrote:
>
> Challenge for the Day :-
> Think Laterally on how to tweak Ruby to be better at One Liners.

I've brought this up a couple of times, but no one seemed to think it a
good idea:

http://groups-beta.google.com/group/comp.lang.ruby/msg/a0595e...
http://groups-beta.google.com/group/comp.lang.ruby/msg/658086...

martin

Neil Stevens

3/8/2005 8:00:00 AM

0

On Tue, 08 Mar 2005 11:20:21 +0900, John Carter wrote:

> On Tue, 8 Mar 2005, ES wrote:
>
>> 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.

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.

--
Neil Stevens - neil@hakubi.us

'A republic, if you can keep it.' -- Benjamin Franklin

Robert Klemme

3/8/2005 11:26:00 AM

0


"John Carter" <john.carter@tait.co.nz> schrieb im Newsbeitrag
news:Pine.LNX.4.61.0503081520160.17689@parore.tait.co.nz...
> On Tue, 8 Mar 2005, Bill Kelly wrote:
>
> > 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