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/

Robbie Clutton

November 05, 2015
Tweet

More Decks by Robbie Clutton

Other Decks in Programming

Transcript

  1. Building command line
    applications in Ruby

    View full-size slide

  2. Let’s build a (simple) calculator

    addition

    subtraction

    multiplication

    View full-size slide

  3. Why?
    We can build small, stand alone applications without worrying about introducing too
    much all at the same time.

    View full-size slide

  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.

    View full-size slide

  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.”

    View full-size slide

  6. ./add.rb 1 2 3

    View full-size slide

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

    View full-size slide

  8. “1” is not a number

    View full-size slide

  9. Add all the numbers
    sum = 0
    ARGV.each do |arg|
    sum += arg.to_i
    end
    puts sum

    View full-size slide

  10. Creating an executable script
    #!/usr/bin/env ruby

    View full-size slide

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

    View full-size slide

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

    View full-size slide