Re: File.join oddity ?

From: "Berger, Daniel" <djberge@...>
Date: 2004-04-26 16:48:18 UTC
List: ruby-core #2820
> Hi !
> 
> By accident I called File.join like this:
> 
>    File.join("a", [["b"], "c"], "d")     # "a/b/c/d"
> 
> and was a rather surprised by the result. This behaviour of 
> "flattening" the arguments seem rather un-intuitive to me. 
> Even more in this case:
> 
>    arr = [["b"], "c"]
>    arr.push arr                  # to get circular
>    File.join("a", arr, "d")      # "a/b/c/b/c/[...]/d"
> 
> 
> As far as I can see, this is *never* what the user would have 
> expected (an error would have been more appropriate).
> 
> Why does File.join have this extra complexity ?
> If this is considered a "feature" I think it should at least 
> be documented.

Extra complexity?  Unintuitive?  This behavior doesn't really surprise
me at all, although you could still argue that it ought to be
documented.  Of course, it begs the question - why are you passing
anything other than a string in the first place?  
 
> Wouldn't it be more natural if File.join only accepted
> strings (or "string-like" objects ) as arguments ?

This sounds like a subtle way of asking for type-checking.  I vote no.

Regards,

Dan


In This Thread

Prev Next