Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby Scripts To Make Life Easier

Ruby Scripts To Make Life Easier

Introduction to subprocesses and how to use them for everyday use.

Paulo Ancheta

December 06, 2016
Tweet

More Decks by Paulo Ancheta

Other Decks in Programming

Transcript

  1. Ruby Scripts to Make Life Easier
    #MakeRubyGreatAgain

    View Slide

  2. @PauloAncheta

    View Slide

  3. We make alias-es all the
    time. We should make
    something more useful.

    View Slide

  4. # talk.rb
    system “echo hello world”
    $ ruby talk.rb
    hello world

    View Slide

  5. # talk*
    #! bin/env ruby
    `echo hello world`
    $ mv talk.rb talk
    $ chmod u+x talk
    $ ./talk
    hello world

    View Slide

  6. # talk*
    #! bin/env ruby
    string = “hello world”
    `echo #{string}`
    $ ./talk
    hello world

    View Slide

  7. # talk*
    #! bin/env ruby
    string = “hello world”
    `echo #{string.gsub(/world/,
    “VanRuby”) } | grep file.txt`
    $ ./talk | more file.txt
    hello VanRuby

    View Slide

  8. $ irb
    irb(main):001:0> files = `ls`
    =>
    “Applications\nDesktop\nDocuments\nDow
    nloads\nLibrary\nMovies\nMusic\nPictur
    es\nPublic\ntalk\n”
    irb(main):002:0> files.class
    => String

    View Slide

  9. irb(main):003:0> system `ls`
    => true

    View Slide

  10. All of that is great. How
    can I be productive
    doe?

    View Slide

  11. # clean-pg*
    #! bin/env ruby
    pg = `ps aux | grep postgres | grep waiting`
    pg = pg.split(/\n/)
    pg.each do |waiting|
    # ... some process to get the pid
    `kill #{pid}`
    end

    View Slide

  12. # split-branch*
    #! bin/env ruby
    # commits => 1z2y3 TICKET-number msg
    specific_commits = `git log - -pretty - -oneline | grep
    #{ARGV}`
    `git checkout master && git pull origin master && git
    checkout -b test-branch`
    specific_commits.each do |commit|
    sha = commit[0..6] # => [123asdf, qwer456, … ]
    `git cherry-pick #{sha}`
    end

    View Slide

  13. #MakeRubyGreatAgain

    View Slide