Professional Web Applications Themes

Dir.chdir '~' - Ruby

Does anyone know why this happens? $ irb irb(main):001:0> Dir.chdir '~' => 0 irb(main):002:0> Dir.chdir '~' Errno::ENOENT: No such file or directory - ~ from (irb):2:in `chdir' from (irb):2 $ ruby -v ruby 1.8.1 (2003-12-25) [i686-linux]...

  1. #1

    Default Dir.chdir '~'


    Does anyone know why this happens?

    $ irb
    irb(main):001:0> Dir.chdir '~'
    => 0
    irb(main):002:0> Dir.chdir '~'
    Errno::ENOENT: No such file or directory - ~
    from (irb):2:in `chdir'
    from (irb):2
    $ ruby -v
    ruby 1.8.1 (2003-12-25) [i686-linux]



    Joel Guest

  2. #2

    Default Re: Dir.chdir '~'

    Joel VanderWerf <Berkeley.EDU> writes:
     

    You must be starting out in a directory with a ~ subdirectory that
    does not itself have a ~ subdirectory. Thus the first time you change
    to directory ~ it succeeds, but the second time it fails.

    $ mkdir \~
    $ irb
    irb(main):001:0> Dir.chdir '~'
    => 0
    irb(main):002:0> Dir.pwd
    => "/home/tim/ruby/~"
    irb(main):003:0> Dir.chdir '~'
    Errno::ENOENT: No such file or directory - ~
    from (irb):3:in `chdir'
    from (irb):3

    In Ruby, you can use use ENV['HOME'], rather than ~, to refer to your
    home directory, if that's what you intended. However, Dir.chdir
    defaults to your home directory, so you don't actually need that in
    this case.

    $ irb
    irb(main):001:0> Dir.pwd
    => "/home/tim/ruby"
    irb(main):002:0> Dir.chdir
    => 0
    irb(main):003:0> Dir.pwd
    => "/home/tim"

    I hope this helps,

    Tim
    Tim Guest

  3. #3

    Default Re: Dir.chdir '~'

    On Fri, Jan 02, 2004 at 09:22:02PM -0500, Tim Heaney wrote: 
    >
    > You must be starting out in a directory with a ~ subdirectory that
    > does not itself have a ~ subdirectory. Thus the first time you change
    > to directory ~ it succeeds, but the second time it fails.[/ref]

    More to the point, I think is that Ruby doesn't expand ~ the way
    the UNIX shells do. What you probably want is Dir.chdir(ENV['HOME']).

    -Mark

    Mark Guest

  4. #4

    Default Re: Dir.chdir '~'

    Tim Heaney wrote: 
    >
    >
    > You must be starting out in a directory with a ~ subdirectory that
    > does not itself have a ~ subdirectory. Thus the first time you change
    > to directory ~ it succeeds, but the second time it fails.[/ref]

    Yep, that was it. Thanks!



    Joel Guest

  5. #5

    Default Re: Dir.chdir '~'

    Mark J. Reed wrote: 
    >>
    >>You must be starting out in a directory with a ~ subdirectory that
    >>does not itself have a ~ subdirectory. Thus the first time you change
    >>to directory ~ it succeeds, but the second time it fails.[/ref]
    >
    >
    > More to the point, I think is that Ruby doesn't expand ~ the way
    > the UNIX shells do. What you probably want is Dir.chdir(ENV['HOME']).[/ref]

    Ok. I guess I have to expand_path if I want that behavior.

    irb(main):001:0> File.expand_path '~'
    => "/home/vjoel"

    Thanks!


    Joel Guest

Similar Threads

  1. Replies: 0
    Last Post: December 26th, 11:07 AM
  2. Replies: 1
    Last Post: December 26th, 10:53 AM
  3. Replies: 7
    Last Post: September 27th, 07:32 AM
  4. problems with chdir()
    By Nicholas Geraldi in forum PHP Development
    Replies: 6
    Last Post: July 23rd, 10:37 PM
  5. Chdir doesn't work under Apache?
    By David in forum PERL Beginners
    Replies: 1
    Last Post: February 17th, 02:43 PM

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
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139