Results 1 to 8 of 8

Thread: need help with ruby: navigating to /home/[user]

  1. #1
    Join Date
    Jun 2006
    Location
    CT - MA - NY, U.S.
    Beans
    1,619

    need help with ruby: navigating to /home/[user]

    Hi,

    I'm learning ruby, and writing my first "usable" program in it. I'm trying to figure out how to get ruby to navigate to/create a directory inside the /home/[user] dir of the user running the app. This is so that I can get it to create a .[config] folder for the app like most common apps do.

    Also, how would I simply get it to recognize which user(s) is currently logged in?

    Just some general instructions would be nice. I'm browsing the rdoc documentation ( http://www.ruby-doc.org/core/ ) right now to see if there's any classes that cover this, but it's a long search, so if anyone knows the answer, please help me out.

  2. #2
    Join Date
    Feb 2007
    Location
    Edinburgh, Scotland
    Beans
    391

    Re: need help with ruby: navigating to /home/[user]

    In bash scripting/terminal "~/" is the home directory, so typing

    Code:
    cd ~/
    takes you straight into the currently logged in users home directory.

  3. #3
    Join Date
    Dec 2006
    Location
    Uk
    Beans
    109

    Re: need help with ruby: navigating to /home/[user]

    You could use environment variables.
    HOME is the users home directory
    USER is the username.

    Environment variables can be access through the ENV variable.

    Code:
    ENV['HOME']
    => "/home/some_user"
    ENV['USER']
    => "some_user"
    I believe ruby also uses the contents of HOME to expand ~ when used in file and directory names.
    OpenStreetMap - Free editable map of the whole world

  4. #4
    Join Date
    Jun 2006
    Location
    CT - MA - NY, U.S.
    Beans
    1,619

    Re: need help with ruby: navigating to /home/[user]

    Quote Originally Posted by winch View Post
    You could use environment variables.
    HOME is the users home directory
    USER is the username.

    Environment variables can be access through the ENV variable.

    Code:
    ENV['HOME']
    => "/home/some_user"
    ENV['USER']
    => "some_user"
    Awesome, thank you. This is exactly what I was looking for!
    Quote Originally Posted by winch View Post
    I believe ruby also uses the contents of HOME to expand ~ when used in file and directory names.
    What do you mean by this comment?

  5. #5
    Join Date
    Apr 2007
    Beans
    78
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: need help with ruby: navigating to /home/[user]

    ~ (tilde) is the variable for the user's home. For example if you type:

    Code:
    echo ~
    into the terminal, it will give you what you want.

    On the interesting side, when you use sudo, the ~ variable doesn't change:

    Code:
    #!/usr/bin/ruby
    
    user=`whoami`
    home=`echo ~`
    if user == "root\n"
        if home == "/root\n"
            puts "Hi, root!"
        else
            puts "Naughty sudoer!"
        end
    else
        puts "You can't use this script because you're not root!"
    end

  6. #6
    Join Date
    Jun 2006
    Location
    CT - MA - NY, U.S.
    Beans
    1,619

    Re: need help with ruby: navigating to /home/[user]

    Quote Originally Posted by JT673 View Post
    ~ (tilde) is the variable for the user's home. For example if you type:

    Code:
    echo ~
    into the terminal, it will give you what you want.

    On the interesting side, when you use sudo, the ~ variable doesn't change:

    Code:
    #!/usr/bin/ruby
    
    user=`whoami`
    home=`echo ~`
    if user == "root\n"
        if home == "/root\n"
            puts "Hi, root!"
        else
            puts "Naughty sudoer!"
        end
    else
        puts "You can't use this script because you're not root!"
    end
    Thanks, I guess that would work too, although the ENV variable does the job as well.

  7. #7
    Join Date
    Apr 2007
    Beans
    78
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: need help with ruby: navigating to /home/[user]

    Yea, I suppose that will make it easier to read...

  8. #8
    Join Date
    Apr 2007
    Beans
    32
    Distro
    Ubuntu 8.10 Intrepid Ibex

    Re: need help with ruby: navigating to /home/[user]

    Note that the "~" mechanism you are using above is not ruby itself performing the expansion, it is the bash shell invoked by the back ticks ``. To get ruby to do it (and to avoid some of the security problems with launching an external shell) try File.expand_path() instead

    Code:
    # using ~ with no expansion produces no output
    p = "~/*"
    Dir.glob(p) => []
    
    # using ENV["HOME"] works
    p = ENV["HOME"] + "/*"
    Dir.glob(p) => [ ... lots of output snipped ... ]
    
    # using Ruby's File.expand_path works too
    p = File.expand_path("~")
    Dir.glob(p) => [ ... lots of output snipped ... ]

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •