O'Reilly logo

Learn Ruby the Hard Way: A Simple and Idiomatic Introduction to the Imaginative World of Computational Thinking with Code, Third Edition, Video Enhanced Edition by Zed A. Shaw

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Exercise 13. Parameters, Unpacking, Variables

Image

In this exercise we will cover one more input method you can use to pass variables to a script (script being another name for your .rb files). You know how you type ruby ex13.rb to run the ex13.rb file? Well, the ex13.rb part of the command is called an “argument.” What we’ll do now is write a script that also accepts arguments.

Type this program, and I’ll explain it in detail:

ex13.rb

 1    first, second, third = ARGV  2  3    puts "Your first variable is: #{first}"  4    puts "Your second variable is: #{second}"  5    puts "Your third variable is: #{third}"

ARGV is ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required