Slide 1

Slide 1 text

Building command line applications in Ruby

Slide 2

Slide 2 text

Let’s build a (simple) calculator ● addition ● subtraction ● multiplication

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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.

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

./add.rb 1 2 3

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

“1” is not a number

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Creating an executable script chmod +x add.rb

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

more?

Slide 14

Slide 14 text

questions?