Tuesday, May 13, 2014

Vocabulary - Learn Ruby the Hard Way

ARGV - a global Array variable which holds, as strings, each argument passed by the shell. (The Terminal) The following program iterates over the ARGV array and prints out its contents. In your Ruby programs, you can access any command-line arguments passed by the shell with the ARGV. http://ruby.about.com/od/rubyfeatures/a/argv.htm
#!/usr/bin/env ruby

ARGV.each do|a|
  puts "Argument: #{a}"
end

# The following is an excerpt of a bash session launching this script (saved as the file test.rb) with a variety of arguments.

$ ./test.rb test1 test2 "three four"
Argument: test1
Argument: test2
Argument: three four /div>

A Short explanation of ARGV http://blog.flatironschool.com/post/64043716616/a-short-explanation-of-argv
ARGV.length.size.count

Empty Array http://ruby.about.com/od/rubyfeatures/a/arrayhash.htm
You can create an empty array by creating a new Array object and storing it in a variable. This array will be empty; you must fill it with other variables to use it. This is a common way to create variables if you were to read a list of things from the keyboard or from a file.

In the following example program, an empty array is created using the array command and the assignment operator. Three strings (ordered sequences of characters) are read from the keyboard and "pushed," or added to the end, of the array.

#!/usr/bin/env ruby

array = Array.new

3.times do
  str = gets.chomp
  array.push str
end

File.open

No comments:

Post a Comment