pmerino
pmerino

Reputation: 6130

How to know with Ruby if a path is outside another?

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!

Upvotes: 0

Views: 50

Answers (1)

Mischa
Mischa

Reputation: 43298

You could use String#starts_with?:

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

And:

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

Upvotes: 3

Related Questions