Take the 2-minute tour ×

I'm building a little script and I'd need to know some way of testing if a path (a string for example) is outside another path (another string). For example:

/some/path and /some/path/file.rb would return false because file.rb is inside /some/path but /some/path and /some/file.rb would return true because file.rb it's outside /some/path.

Thanks in advance!

share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

You could use String#starts_with?:

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
share|improve this answer
add comment

Your Answer


