本文介绍了File.join 什么时候有用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
通过阅读文档,很明显 File.join 将给定的参数与/字符连接起来.
什么时候使用这个,相对于 filenames.join('/')
,有益吗?
From reading the documentation, it's apparent that File.join joins the given parameters with the / character.
When is using this, as opposed to filenames.join('/')
, beneficial?
推荐答案
还有一个细微的区别:
File.join('foo','bar')
#=> "foo/bar"
['foo','bar'].join('/')
#=> "foo/bar"
但是,如果您传递的参数已经以 /
结尾(这在处理路径时很常见),则结果中不会有两个斜杠:
But, if you pass an argument already ending with /
(which is quite often when working with paths), you won't have two slashes in the result:
File.join('foo/','bar')
#=> "foo/bar"
['foo/','bar'].join('/')
#=> "foo//bar"
这篇关于File.join 什么时候有用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!