test/netrc_test.rb
def fallback_homedir_else_pwd
    begin
      require 'etc'
    rescue LoadError
      # Without the 'Etc' module we are unable to query the password database
      return Dir.pwd
    end

    # Note that Process.uid returns the value of getuid() which (on Linux) may
    # be different from the value of /proc/self/loginuid (e.g., for a process
    # that is not a (grand)child of a login process). That is important here
    # because we should be able to obtain the record from the password
    # database, even if Ruby's built-in 'Dir.home' cannot (because it uses
    # getlogin() and fails in that scenario, at least through Ruby 2.7.1).
    #
    passwd_record = Etc.getpwuid(Process.uid)
    unless passwd_record
      return Dir.pwd  # Record for uid not available for some reason
    end

    return passwd_record.dir if File.readable?(passwd_record.dir)

    return Dir.pwd
  end
# Helper for obtaining a value for the user's home directory from the
# password database (querying it by uid), else falling back on using the
# pwd. This is analogous to what the Netrc.home_path method does on
# Unix-like systems when neither 'NETRC' nor 'HOME' are defined, if the
# process is not a child (or grandchild) of a login session, or if the
# user's actual home directory is not readable for some reason.
#
Helper for obtaining a value for the user's home directory from the
password database (querying it by uid), else falling back on using the
pwd.
Helper for obtaining a value for the user ' s home directory from the password database ( querying it by uid ) else falling back on using the pwd .
def fallback_homedir_else_pwd
    begin
      require 'etc'
    rescue LoadError
      return Dir.pwd
    end
    passwd_record = Etc.getpwuid(Process.uid)
    unless passwd_record
      return Dir.pwd   Record for uid not available for some reason
    end
    return passwd_record.dir if File.readable?(passwd_record.dir)
    return Dir.pwd
  end
def fallback_homedir_else_pwd begin require 'etc' rescue LoadError return Dir . pwd end passwd_record = Etc . getpwuid ( Process . uid ) unless passwd_record return Dir . pwd end return passwd_record . dir if File . readable? ( passwd_record . dir ) return Dir . pwd end
Helper for obtaining a value for the user's home directory from the
password database (querying it by uid), else falling back on using the
pwd.
Helper for obtaining a value for the user ' s home directory from the password database ( querying it by uid ) else falling back on using the pwd .
# Without the 'Etc' module we are unable to query the password database # Note that Process.uid returns the value of getuid() which (on Linux) may # be different from the value of /proc/self/loginuid (e.g., for a process # that is not a (grand)child of a login process). That is important here # because we should be able to obtain the record from the password # database, even if Ruby's built-in 'Dir.home' cannot (because it uses # getlogin() and fails in that scenario, at least through Ruby 2.7.1). # # Record for uid not available for some reason