Matthew,

   Well something is inconsistent  somewhere.

   However, the reason this came up was I
wrote a class to perform a tree scan.  It recursively
stepped through to directory tree.  It failed when
it ran across a directory that had a number of directories
prefixed with a  tilde '~'. 

When you have a dir containing this
  dir1:
    ~dira  ~dirb  ~dirc

  Then you list the 'files' in a dir and
then recursively call expand_path on each
dir you find and agin list the files--
this failure stops the scan.

I want to make it consistent with the way the
Bash Shell works.

mkdir ~dir1   produces "./~dir" NOT "/home/dir"

Am I wrong here?


Matthew Boeh wrote:
> On Sat, Jul 19, 2008 at 04:27:09AM +0900, C.E. Thornton wrote:
>   
>> Looking at the code I see that, However:
>>
>>  When the path ~name it by definition IS NOT A VALID  pathname.
>>
>>  FOR EXAMPLE:   if your home dir is "/home"
>>
>>  In your home directory make the following directories
>>   "/home/test" and "/home/~test"
>>
>>  Check these results:
>>
>>   cd test     ==> /home/test
>>   cd ~test   ==> /home/~test      # Note this interpreted as "/home/~test"
>>                                                   # NOT /home/test !
>>   cd ~/test  ==> /home/test
>>
>>   So as you can see -- It appears that Ruby mishandles path expansion!
>>
>>     
>
> The logic, as implemented by the Bourne shell, is that when a tilde is 
> encountered as an element of a path, it's expanded to the home directory of 
> the user. If the tilde is followed immediately by the name of a user, it's 
> expanded to that user's home directory. If the tilde is followed by something 
> other than a path separator that is not the name of a user, it isn't expanded 
> at all. In any case, the tilde can be escaped with a backslash. This is a 
> macro expansion performed regardless of whether a directory actually named 
> ~whatever exists, as shown below.
>
> It's not a Linux/Unix thing per se, but a behavior of the Bourne shell that's 
> been adopted in other contexts.
>
> Demonstration in DASH, a POSIX-compliant Bourne shell:
>
> $ whoami
> mboeh
> $ echo $HOME
> /home/mboeh
> $ echo ~
> /home/mboeh
> $ echo ~mboeh
> /home/mboeh
> $ echo ~root
> /root
> $ echo ~root/mysterious_directory
> /root/mysterious_directory
> $ echo \~mboeh
> ~mboeh
> $ mkdir \~root
> $ echo ~root
> /root
> $ echo ~nosuchuser
> ~nosuchuser
>
> That said, it appears File#expand_path is inconsistent with the Bourne shell 
> in that "~nosuchuser" raises an exception rather than just leaving the tilde 
> unexpanded.
>
>   


-- 
Competency and chastity have much in common,
they both encompass their own punishment! 
 
-- C.E. Thornton -- Hawthorne Press --