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

Building command line applications in Ruby

Building command line applications in Ruby

Presented at Women Who Code, Rails install fest.

http://www.meetup.com/Women-Who-Code-London/events/225878453/

1668868370ee5829339e06031ad0b145?s=128

Robbie Clutton

November 05, 2015
Tweet

More Decks by Robbie Clutton

Other Decks in Programming

Transcript

  1. Building command line applications in Ruby

  2. Let’s build a (simple) calculator • addition • subtraction •

    multiplication
  3. Why? We can build small, stand alone applications without worrying

    about introducing too much all at the same time.
  4. Reading from the command line We can read numbers from

    the command line, and put them into our application to do addition, subtraction and multiplication upon.
  5. ARGV “ARGF is a stream designed for use in scripts

    that process files given as command-line arguments or passed in via STDIN. The arguments passed to your script are stored in the ARGV Array, one argument per element. ARGF assumes that any arguments that aren't filenames have been removed from ARGV.”
  6. ./add.rb 1 2 3

  7. ARGV == [“1”, “2”, “3”]

  8. “1” is not a number

  9. Add all the numbers sum = 0 ARGV.each do |arg|

    sum += arg.to_i end puts sum
  10. Creating an executable script #!/usr/bin/env ruby

  11. Creating an executable script chmod +x add.rb

  12. Putting it all together ./add.rb 1 2 3 => 6

  13. more?

  14. questions?