- Array
- # a trailing comma is ignored
name = [‘Satish’, ‘Talim’, ‘Ruby’, ‘Java’,] - an example of returning an array
def mtdarry(num)
square = num * num
return num, square if num > 5
end
# using parallel assignment to collect the return value
num, square = mtdarry (6)
puts num => 6
puts square => 36
- Array and range (a sequence)
[“a”, “b”]===”a” => false
(‘a’..’b’) === ‘a’ => true #see if some value falls in the interval represented by the range - An environment variable is a link between our program and the outside world. You can access operating system environment variables using the predefined variable ENV. ENVis simply a hash.
ENV.each {|k,v| puts “#{k}: #{v}”}
-
Library GetoptLong
Class GetoptLong supports command-line option parsing. Options may be a minus sign (-) followed by a single character, or two minus signs (–) followed by a name (a long option). Options may be given in any order. A single internal option may have multiple external representations. For example, the option to control verbose output could be any of -v, –verbose, or –details. Some options may also take an associated value. Each internal option is passed to GetoptLong as an array, containing strings representing the option’s external forms and a flag. The flag specifies how GetoptLong is to associate an argument with the option (NO_ARGUMENT, REQUIRED_ARGUMENT, or OPTIONAL_ARGUMENT).
Suppose I want to call a Ruby program as:
ruby tsftpc.rb -h ftp.ibiblio.org -n 21 -u anonymous -p s@s.comHere’s the code to do so:
- require ‘getoptlong’
- unless ARGV.length == 4
- puts “Usage: ruby tsftpc.rb -h ftp_site_url -n port_no -u user_name -p password”
- exit
- end
- host_name = port_no = user_name = password = ”
- # specify the options we accept and initialize
- # the option parser
- opts = GetoptLong.new(
- [ “–hostname”, “-h”, GetoptLong::REQUIRED_ARGUMENT ],
- [ “–port”, “-n”, GetoptLong::REQUIRED_ARGUMENT ],
- [ “–username”, “-u”, GetoptLong::REQUIRED_ARGUMENT ],
- [ “–pass”, “-p”, GetoptLong::REQUIRED_ARGUMENT ]
- )
- # process the parsed options
- opts.each do |opt, arg|
- case opt
- when ‘–hostname’
- host_name = arg
- when ‘–port’
- port_no = arg
- when ‘–username’
- user_name = arg
- when ‘–pass’
- password = arg
- end
- end
- Ruby automatically places any parameters that are appended to the command line when you launch your Ruby program into a special array called ARGV. If your program tmp.rb is:
f = ARGV[0]
puts fYou can execute this program from the command line as:
ruby tmp.rb 23The program should display 23.
- a=nil || “hi”
a=> “hi”
a=nil or “hi”
a => nil
the reason is that the predence from high to low is ||, =, or
- Ruby lets you have a comma-separated list of rvalues. Once Ruby sees more than one rvalue in an assignment, the rules of parallel assignmentcome into play. First, all the rvalues evaluated, left to right, and collected into an array (unless they are already an array). This array will be the eventual value returned by the overall assignment. Next, the left hand side (lhs) is inspected. If it contains a single element, the array is assigned to that element.
a = 1, 2, 3, 4 # => a == [1, 2, 3, 4]
b = [1, 2, 3, 4] # => b == [1, 2, 3, 4]If the lhs contains a comma, Ruby matches values on the rhs against successive elements on the lhs. Excess elements are discarded.
a, b = 1, 2, 3, 4 # => a == 1, b == 2
c, = 1, 2, 3, 4 # => c == 1