ruby notes(3)

  • Array
  1. # a trailing comma is ignored
    name = [‘Satish’, ‘Talim’, ‘Ruby’, ‘Java’,]
  2. 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.com

    Here’s the code to do so:

    1. require ‘getoptlong’
    2. unless ARGV.length == 4
    3.   puts “Usage: ruby tsftpc.rb -h ftp_site_url -n port_no -u user_name -p password”
    4.   exit
    5. end
    6. host_name = port_no = user_name = password = ”
    7. # specify the options we accept and initialize
    8. # the option parser
    9. opts = GetoptLong.new(
    10. [ “–hostname”, “-h”, GetoptLong::REQUIRED_ARGUMENT ],
    11. [ “–port”, “-n”, GetoptLong::REQUIRED_ARGUMENT ],
    12. [ “–username”, “-u”, GetoptLong::REQUIRED_ARGUMENT ],
    13. [ “–pass”, “-p”, GetoptLong::REQUIRED_ARGUMENT ]
    14. )
    15. # process the parsed options
    16. opts.each do |opt, arg|
    17.   case opt
    18.     when ‘–hostname’
    19.       host_name = arg
    20.     when ‘–port’
    21.       port_no = arg
    22.     when ‘–username’
    23.       user_name = arg
    24.     when ‘–pass’
    25.       password = arg
    26.   end
    27. 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 f

    You can execute this program from the command line as:

    ruby tmp.rb 23

    The 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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s