Cross-platform means of getting user's home directory in Ruby?

前端 未结 5 525
情深已故
情深已故 2021-02-03 16:47

Java has the convienient System.getProperty(\"user.home\") to get the user\'s \"home\" directory in a platform-independent way. What\'s the equivalent in Ruby? I

5条回答
  •  感动是毒
    2021-02-03 17:41

    ENV["HOME"] or ENV["HOMEPATH"] should give you what you want.

    homes = ["HOME", "HOMEPATH"]
    
    realHome = homes.detect {|h| ENV[h] != nil}
    
    if not realHome
       puts "Could not find home directory"
    end
    

提交回复
热议问题