2013-02-06 35 views
0

我正在構建一個小腳本,我需要知道某種方式的測試,如果一個路徑(例如一個字符串)超出另一個路徑(另一個字符串)。例如:如何知道Ruby是否有路徑在另一個之外?

/some/path/some/path/file.rb將返回false因爲file.rb裏面/some/path/some/path/some/file.rb將返回true因爲file.rb它的外面/some/path

在此先感謝!

回答

3

你可以使用:

path = '/some/path' 
file = '/some/path/file.rb' 
file.starts_with?(path) #=> true 

和:

path = '/some/path' 
file = '/some/file.rb' 
file.starts_with?(path) #=> false 
相關問題