Slide 1

Slide 1 text

Ruby > Shell Scripts By Tristan Hume

Slide 2

Slide 2 text

Ruby without the web! Automate ALL the things!

Slide 3

Slide 3 text

Ruby + Unix = :D

Slide 4

Slide 4 text

A Smooth Transition ● Backticks: `ps -ax`.lines.map(&:split) ● Handy Methods: ○ File.foreach("words.txt") {|line| puts line.capitalize} ● Dir[]: ○ Dir['**/*.rb'].each {|file| code_files << file }

Slide 5

Slide 5 text

But I like all those little utilities...

Slide 6

Slide 6 text

Have No Fear: FileUtils is here! require "fileutils" FileUtils.cp some_file, some_file + ".bak" FileUtils.rm_rf "trash" FileUtils.ln_s 'verylongsourcefilename.c', 'c', :force => true FileUtils.chown nil, 'bin', Dir['/usr/bin/*'], :verbose => true

Slide 7

Slide 7 text

In Which Ruby's Scripting Prowess Becomes Fully Apparent. += :Ruby_Awesomness

Slide 8

Slide 8 text

My All-Time Favorite Little Gem gem install progress

Slide 9

Slide 9 text

Computing: 12.35% (ETA: 2.4m) >> data.map.with_progress("Computing...") {|item| ...

Slide 10

Slide 10 text

Awesomeness Comes In Many Forms data.with_progress do |item| data.with_progress.map do |item| # Any enumerable and any enumerable method (1..100000).with_progress.each_cons do |item| 10.times_with_progress('Counting to 10') do |i| (1..10).with_progress('Outer').map do |a| (1..10).with_progress('Middle').map do |b| # Nesting! end end

Slide 11

Slide 11 text

An exhilarating wrapper for all things shell. (you can use IRB as your login shell if you really want to...) gem install rush

Slide 12

Slide 12 text

require 'rush' file = Rush['/tmp/myfile'] file.write "hello" puts file.contents file.destroy puts Rush.my_process.pid puts Rush.processes.size puts Rush.bash("echo SHELL COMMAND | tr A-Z a-z") puts Rush.launch_dir['*.rb'].search(/Rush/).entries.inspect # Remote Execution local = Rush::Box.new('localhost') remote = Rush::Box.new('my.remote.server.com') local_dir = local['/Users/adam/myproj/'] remote_dir = remote['/home/myproj/app/']

Slide 13

Slide 13 text

Another hardcore Ruby shell. gem install chitin

Slide 14

Slide 14 text

$ chitin ~/src/chitin % ls ~/src/chitin % ll = ls -:al ~/src/chitin % hg.stat ~/src/chitin % hg.stat | wc | L {|i| i.size } ~/src/chitin % git.status > '/tmp/test.out' ~/src/chitin % 44 - 2 ~/src/chitin % _ + 5 ~/src/chitin % wget "http://thume.ca/" ~/src/chitin % _ # run the same command again # You can also use Chitin as a library Shell Commands in Valid Ruby

Slide 15

Slide 15 text

Better than no GUI at all. Bonus: Crappy Tk GUIs

Slide 16

Slide 16 text

"This is all crappy Unix stuff, what's in it for me?" - Possibly some audience member Using Windows?

Slide 17

Slide 17 text

WIN32OLE FTW! require 'win32ole' excel = WIN32OLE.new('Excel.Application') excel.visible = true workbook = excel.Workbooks.Add(); worksheet = workbook.Worksheets(1); worksheet.Range("A1:D1").value = ["North"," South","East","West"]; worksheet.Range("A2:B2").value = [5.2, 10]; worksheet.Range("C2").value = 8; worksheet.Range("D2").value = 20;

Slide 18

Slide 18 text

Thank God. Never touch Visual Basic for Applications Again!

Slide 19

Slide 19 text

In which WIN32OLE and Tk GUI saves the day. Case Study

Slide 20

Slide 20 text

TL;DR ● Ruby has awesome utilities for scripting ● There are some great gems like "progress" to help. ● Never touch Visual Basic or Bash Scripting again, you won't regret it.

Slide 21

Slide 21 text

Tristan Hume http://thume.ca/ Thanks