Rethinking the Generic Ruby Oneliner...

Discussion in 'Ruby' started by John Carter, Mar 25, 2007.

  1. John Carter

    John Carter Guest

    So I was asking myself the question... What ruby run time options
    would I add to make oneliners easier.

    Basically awk started it all with the notion of a loop that iterated
    over a file allowing patterns and actions per line...

    This is enshrined in ruby as..
    ruby -nple 'do whatever with $_'
    or...
    ruby -nplae 'do whatever with $_ split into $F'

    However, the ruby variants...
    IO.read( filename).gsub( %r{whatever}mx){|match| }
    and
    IO.read( filename).scan( %r{whatever}mx){|match| }
    are way more powerful.

    The next Big Thing in one liners was the perl -i.bak option. Ruby has it too.

    Invariably the -i.bak option is used in conjunction with the "find" and "xargs"
    commands to find the files.

    Like so...
    find . -name '*.[hc]' | xargs ruby -i.bak -nple 'whatever'

    But Ruby has an excellent and way less cryptic find facility...

    require 'find'

    Find.find( startPath) do |path|
    next unless path =~ %r{ \.[hc] $ }x
    # Do something with path
    end

    Wouldn't it be nice if we could ...

    ruby --find startPath --matching pathRegex --scan scanRegex -e action
    or
    ruby --find startPath --matching pathRegex -i.bak --gsub searchRegex -e "replaceString"

    with suitable one letter '-' options replacing the --long options for golfing.

    ruby --find startPath --matching pathRegex --prune 'pruneRegex' --scan scanRegex -e action

    Would effectively expand to something vaguely like...
    require 'find'
    Find.find( *startPath.split(',')) do |path|
    next unless path.match( pathRegex)
    Find.prune if path.match( pruneRegex)
    IO.read(path).scan(scanRegex){|match| action}
    end

    and...
    ruby --find startPath --matching pathRegex -i.bak --gsub searchRegex -e "replaceString"

    Would effectively expand to something vaguely like...
    require 'find'
    Find.find( *startPath.split(',')) do |path|
    next unless path.match( pathRegex)
    Find.prune if path.match( pruneRegex)
    backup = path + '.bak'
    File.rename( path, backup)
    open( path, 'w') do |outf|
    outf.syswrite( IO.read( backup).gsub(searchRegex){|match| replaceString})
    end
    end

    Any suggestions before I write up an RCR?

    John Carter Phone : (64)(3) 358 6639
    Tait Electronics Fax : (64)(3) 359 4632
    PO Box 1645 Christchurch Email :
    New Zealand
    John Carter, Mar 25, 2007
    #1
    1. Advertising

  2. Maybe:
    >> ruby -rmylibrary -e "replaceString" -- --find startPath --matching

    pathRegex -i.bak --gsub searchRegex

    If that's too long, maybe a shell/program wrapper ^^
    >> myruby --find startPath --matching pathRegex -i.bak --gsub

    searchRegex -e "replaceString"

    I like magic best when I can tear it apart -- and you have all the
    tools now to put it to the test.


    Paul
    Paul Stickney, Mar 25, 2007
    #2
    1. Advertising

  3. On Mon, Mar 26, 2007 at 07:55:05AM +0900, Paul Stickney wrote:
    > Maybe:
    > >>ruby -rmylibrary -e "replaceString" -- --find startPath --matching pathRegex -i.bak --gsub searchRegex

    >
    > If that's too long, maybe a shell/program wrapper ^^
    > >>myruby --find startPath --matching pathRegex -i.bak --gsub searchRegex -e "replaceString"


    If you call it x.rb then you can have ruby -rx ...
    Brian Candler, Mar 26, 2007
    #3
  4. On 26.03.2007 08:00, Brian Candler wrote:
    > On Mon, Mar 26, 2007 at 07:55:05AM +0900, Paul Stickney wrote:
    >> Maybe:
    >>>> ruby -rmylibrary -e "replaceString" -- --find startPath --matching pathRegex -i.bak --gsub searchRegex

    >> If that's too long, maybe a shell/program wrapper ^^
    >>>> myruby --find startPath --matching pathRegex -i.bak --gsub searchRegex -e "replaceString"

    >
    > If you call it x.rb then you can have ruby -rx ...


    Or create a shell script ("rubyfind") that does the command line
    parameter interpretation. You could even implement the shell script in
    ruby making your life much easier. :)

    Kind regards

    robert
    Robert Klemme, Mar 30, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Magnus Lycka

    Rethinking the Python tutorial

    Magnus Lycka, Feb 9, 2006, in forum: Python
    Replies:
    11
    Views:
    529
    Magnus Lycka
    Feb 15, 2006
  2. stork
    Replies:
    6
    Views:
    344
    Michael Ashton
    Dec 18, 2006
  3. dorayme
    Replies:
    0
    Views:
    370
    dorayme
    May 4, 2008
  4. Steven D'Aprano
    Replies:
    138
    Views:
    1,941
  5. mrpink
    Replies:
    7
    Views:
    144
    mrpink
    Apr 20, 2007
Loading...

Share This Page