Ruby Forum Ruby-core > NEW Command: absolute_path() --

Posted by C.E. Thornton (Guest)
on 16.08.2008 23:05
(Received via mailing list)
Core,

I have created patches for both 1.8.7 and 1.9.0 trunk
to implement the command 'absolute_path()'.

The documentation for the command follows:

/*
 *  call-seq:
 *     File.absolute_path(file_name [, dir_string] ) -> abs_file_name
 *
 *  Converts a pathname to an absolute pathname. Relative paths are
 *  referenced from the current working directory of the process unless
 *  <i>dir_string</i> is given, in which case it will be used as the
 *  starting point. If the given pathname starts with a 
``<code>~</code>''
 *  it is NOT expanded, it is treated as a normal directory name.
 *
 *     File.absolute_path("~oracle/bin")       #=>
"<relative_path>/~oracle/bin"
 */

If there is anything else required for inclusion approval,
please let me know.

Chuck T.
Posted by Yukihiro Matsumoto (Guest)
on 18.08.2008 04:09
(Received via mailing list)
Hi,

In message "Re: [ruby-core:18319] NEW Command:  absolute_path() --"
    on Sun, 17 Aug 2008 06:01:52 +0900, "C.E. Thornton" 
<admin@hawthorne-press.com> writes:

|I have created patches for both 1.8.7 and 1.9.0 trunk
|to implement the command 'absolute_path()'.

|If there is anything else required for inclusion approval,
|please let me know.

I am still thinking to decide if this is a creeping feature, or useful
addition (since it's VERY similar to a existing method).  I just want
to tell you you're not ignored at all.

              matz.
Posted by Thomas Sawyer (7rans)
on 18.08.2008 06:06
(Received via mailing list)
On Aug 17, 10:03 pm, Yukihiro Matsumoto <m...@ruby-lang.org> wrote:
>
> I am still thinking to decide if this is a creeping feature, or useful
> addition (since it's VERY similar to a existing method).  I just want
> to tell you you're not ignored at all.

Why does the ~ prevent the expansion in File.expand_path?

  File.expand_path('~oracle/bin')  #=> '~oracle/bin'

Never noticed this before.

                                                             7rans.
Posted by Michael Greenly (mgreenly)
on 18.08.2008 15:00
(Received via mailing list)
Are you sure you didn't mean to use "~/oracle/bin"
Posted by Luis Lavena (luislavena)
on 18.08.2008 16:40
(Received via mailing list)
On Mon, Aug 18, 2008 at 2:56 PM, michael greenly <mgreenly@gmail.com> 
wrote:
> Are you sure you didn't mean to use "~/oracle/bin"
>

The problem is that File.expand_path asumes if no "~" was provided,
current directory (Dir.pwd) is starting point:

irb(main):007:0> Dir.pwd
=> "D:/Users/Luis"
irb(main):008:0> File.expand_path("~")
=> "D:/Users/Luis"
irb(main):009:0> File.expand_path(".")
=> "D:/Users/Luis"
irb(main):010:0> File.expand_path("..")
=> "D:/Users"
irb(main):011:0> File.expand_path("./~oracle")
=> "D:/Users/Luis/~oracle"
irb(main):012:0> File.expand_path("~oracle")
=> "~oracle"

In the last line you can see the problem... it doesn't expand the path
if ~ is found in as part of the path to expand.

--
Luis Lavena
AREA 17
-
Human beings, who are almost unique in having the ability to learn from
the experience of others, are also remarkable for their apparent
disinclination to do so.
Douglas Adams
Posted by Nobuyoshi Nakada (nobu)
on 18.08.2008 20:17
(Received via mailing list)
Hi,

At Mon, 18 Aug 2008 23:36:27 +0900,
Luis Lavena wrote in [ruby-core:18324]:
> irb(main):012:0> File.expand_path("~oracle")
> => "~oracle"
> 
> In the last line you can see the problem... it doesn't expand the path
> if ~ is found in as part of the path to expand.

It should raise an exception, and this is the point of OP.

$ ruby18 -v -e 'p File.expand_path("~oracle")'
ruby 1.8.7 (2008-08-18 revision 18677) [i686-linux]
-e:1:in `expand_path': user oracle doesn't exist (ArgumentError)
  from -e:1
$ ruby19 -v -e 'p File.expand_path("~oracle")'
ruby 1.9.0 (2008-08-18 revision 18687) [i686-linux]
-e:1:in `expand_path': user oracle doesn't exist (ArgumentError)
  from -e:1:in `<main>'
Posted by C.E. Thornton (Guest)
on 19.08.2008 08:52
(Received via mailing list)
Trans wrote:
>> |If there is anything else required for inclusion approval,
>
> Never noticed this before.
>
>                                                              7rans.
>
>
>   
Yeah, that got me too!

When expand_path() expands a path containing a leading tilde(~)
AND the directory name associated with it is a USER NAME in
the system it ASSUMES you specifying the home area of that user!

For example,  if I am in '/home/thornton/test' and it contains a dir
'~thornton' expand_path() returns '/home/thornton' NOT
'/home/thornton/test/~thornton'

I ran into fhis head first while writing a program to scan all
directories.

absolute_path() would ignore this wonderful feature and return
'/home/thornton/test/~thornton'.

Chuck T.
Posted by C.E. Thornton (Guest)
on 19.08.2008 09:02
(Received via mailing list)
Nobuyoshi Nakada wrote:
>>     
>   from -e:1:in `<main>'
>
>   
The example was used because it is the one of the examples in the
documentation
for File.expand_path()

If there is a user named 'oracle' on the system being used, then file
expand_path
will always return  it's home dir say '/home/oracle'.

If you are in a dir named '/home/user/test' and it contains a dir
'~oracle' then
calling expand_path("~oracle") while in that area returns '/home/oracle' 
NOT
'/home/user/test/~oracle'

Chuck T.
Posted by C.E. Thornton (Guest)
on 19.08.2008 09:03
(Received via mailing list)
Yukihiro Matsumoto wrote:
>
> I am still thinking to decide if this is a creeping feature, or useful
> addition (since it's VERY similar to a existing method).  I just want
> to tell you you're not ignored at all.
>
>               matz.
>
>
>   
Thank you for your reply!

Chuck T.
Posted by Thomas Sawyer (7rans)
on 19.08.2008 18:50
(Received via mailing list)
On Aug 19, 2:48 am, "C.E. Thornton" <ad...@hawthorne-press.com> wrote:

> Yeah, that got me too!
>
> When expand_path() expands a path containing a leading tilde(~)
> AND the directory name associated with it is a USER NAME in
> the system it ASSUMES you specifying the home area of that user!
>
> For example,  if I am in '/home/thornton/test' and it contains a dir
> '~thornton' expand_path() returns '/home/thornton' NOT
> '/home/thornton/test/~thornton'

Is this common practice? I mean it seems like a sucky edge case to
have to fuss with. Why not just have a separate method for that, say

  File.home_path('thornton')

I don't mind '~/' converting to HOME so much, but this USER NAME thing
seems too much, as indicated by the need of yet another method
#absolute_path.

MO,
T.
Posted by Jim Weirich (Guest)
on 19.08.2008 20:51
(Received via mailing list)
On Aug 19, 2008, at 12:46 PM, Trans wrote:

> Is this common practice?

Yes.  Very much so.
Posted by Pit Capitain (Guest)
on 19.08.2008 21:43
(Received via mailing list)
2008/8/19 Trans <transfire@gmail.com>:
> Is this common practice? I mean it seems like a sucky edge case to
> have to fuss with. Why not just have a separate method for that, say
>
>  File.home_path('thornton')

+1

Though it might be common unix practice, Ruby could/should have a more
explicit way to get at the home path of a user. I think there's no
need to encode this in a file naming scheme. Can anyone show me a use
case where it would be more appropriate to have the existing behaviour
instead of a separate method?

Regards,
Pit
Posted by Matthew Boeh (Guest)
on 19.08.2008 22:00
Attachment: signature.asc (190 Bytes)
(Received via mailing list)
On Wed, Aug 20, 2008 at 04:39:45AM +0900, Pit Capitain wrote:
> need to encode this in a file naming scheme. Can anyone show me a use
> case where it would be more appropriate to have the existing behaviour
> instead of a separate method?

User input:

  config = YAML.load(File.read(config_file_path))
  destination_path = File.expand_path(config["destination_path"])

I've always understood File.expand_path to be intended for this kind of 
use,
where a user might plausibly expect to be able to use the kind of 
expansions
they can in the Bourne shell.
Posted by Pit Capitain (Guest)
on 19.08.2008 22:32
(Received via mailing list)
2008/8/19 Matthew Boeh <mboeh@desperance.net>:
> I've always understood File.expand_path to be intended for this kind of use,
> where a user might plausibly expect to be able to use the kind of expansions
> they can in the Bourne shell.

Thanks Matthew. I always understood #expand_path to just create an
absolute path, and that expanding user home dirs was only a side
effect, but I can see that the word "expand" could have a different
meaning here. Guess it always depends on what OSes and with which
tools you've worked before.

So maybe another method #absolute_path isn't that bad an idea, because
of the problems Chuck has run into. I'm glad I don't have to decide
whether it is worth the new method.

Regards,
Pit