Skip to content

Latest commit

 

History

History
69 lines (50 loc) · 4.4 KB

ruby_study_note.md

File metadata and controls

69 lines (50 loc) · 4.4 KB
#! /usr/bin/env ruby      ### go to /usr/bin, execute env then load ruby, ruby shall be installed at /usr/bin/

$-w = nil                ### $-w warning level in ruby, set nil means no warning

class << File
    alias :_exists? : exists?
    def File.exist?( arg )
        _exists? arg or symlink? arg
    end
end

require optparse
require fileutils

trap( "SIGINT"){ # single handler... SIGINT = ctrl+c 
    trap("SIGINT") {}
    puts "(W) Let me finish or this is hosed(cleaned up by hose)" # output a msg... 

}

vars = {             # a hash vars will have 2 keys: lib and dir, which have empty array as their value... 
    :lib => [],
    :dir => []
}
ARGV.options do | opts | ### 
    begin
        opts.banner = "Usage: #$0 [options]\n"
        opts.on("-d", "--directory=DIR", "Root source dir."                   ) {|v| vars[ :root] = v}
        opts.on("-r", "--remove", "Remove lib or dir. May not use with -u."   ) {|v| vars[ :rem ]=v}
        opts.on( "-q", "--quiet",         "No console output."                ) {|v| vars[ :quiet  ] = v } 
        opts.on("-u", "--update", "Update missing links. May not use with -r.") {|v| vars[ :update ]=v}
        opts.parse!
        raise opts.to_s if var[:rem] and vars[ :update ]
    rescue
        puts $!
        exit(1)
    end
end

The code you provided is defining a new method exists? for the File class in Ruby, while preserving the original behavior of the exists? method by aliasing it as _exists?. class << File: This syntax opens up the singleton class (also known as the eigenclass or metaclass) of the File class. It allows defining methods specific to the singleton class of File, which affects only the File class itself, not its instances.

alias :_exists? :exists?: This line creates an alias for the existing method exists? of the File class. It renames the original exists? method as _exists?. This aliasing ensures that the original behavior of exists? is preserved.

def File.exists?(arg): This line defines a new method exists? within the singleton class of File. The method takes an argument arg.

_exists? arg or symlink? arg: Inside the newly defined exists? method, it calls the aliased _exists? method with the provided argument arg. The or operator is used to add additional behavior. If the original _exists? method returns false (indicating the file does not exist), it calls the symlink? method with the same argument arg. The symlink? method is a built-in Ruby method that checks if a file is a symbolic link.

By defining the File.exists? method in this way, it overrides the default behavior of File.exists? and adds an extra condition to also check if the file is a symbolic link.

ARGV.options do |opts|: This starts the definition of the command-line options using the ARGV.options method. It provides a block with a parameter opts, representing the OptionParser object.

opts.banner = "Usage: #$0 [options]\n": This sets the usage banner for the command-line options, indicating how to use the script along with the available options.

opts.on(...) lines: These lines define the individual options and their corresponding behaviors. For example:

opts.on("-d", "--directory=DIR", "Root source dir.") {|v| vars[:root] = v}: This defines the option -d or --directory=DIR to specify the root source directory. The value passed with this option will be assigned to vars[:root]. Similarly, other options like -r, -q, and -u are defined with their corresponding behaviors and assignments to the vars hash. opts.parse!: This line parses the command-line arguments and options provided by the user. It triggers the processing of the options and assigns their values to the respective keys in the vars hash.

raise opts.to_s if vars[:rem] && vars[:update]: This line checks if both the :rem and :update keys in the vars hash are set. If so, it raises an exception with the message generated by opts.to_s. This ensures that the -r and -u options are not used together.

rescue: This block catches any exceptions that occur during the option parsing process.

puts $! and exit(1): If an exception is raised, this code outputs the error message stored in the global variable $! (which contains the last raised exception) and exits the program with a status code of 1.

In summary, this code sets up command-line options using OptionParser, assigns the values provided by the user to the vars hash, and handles any errors that occur during the option parsing process.