Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Viewing the parent file path (See related posts)

The following snippet demonstrates how to retrieve just a parent file path (which may be a blank string).

f = "fun"           # 1)
f = "fun/123"       # 2)
f = "great/fun/123" # 3)

parent_path = f.sub(/\/?[^\/]+$/,'')

# or

parent_path = f.split('/')[0..-2].join('/')


output:

# 1) => ""
# 2) => "fun"
# 3) => "great/fun"


*update: 11-Nov-09 @ 7:30pm*

Here's another regex alternative:
parent_path = f[/.+(?=\/)/].to_s

You need to create an account or log in to post comments to this site.


Click here to browse all 7207 code snippets

Related Posts