Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easiest way to convert "a/b/c" to ["a/b/c", "a/b", "a"]

Tags:

regex

ruby

In Ruby, I'd like to convert a slash-separate String such as "foo/bar/baz" into ["foo/bar/baz", "foo/bar", "foo"]. I already have solutions a few lines long; I'm looking for an elegant one-liner. It also needs to work for arbitrary numbers of segments (0 and up).

like image 741
Yehuda Katz Avatar asked Feb 13 '10 09:02

Yehuda Katz


People also ask

What is ratio formula?

If you are comparing one data point (A) to another data point (B), your formula would be A/B. This means you are dividing information A by information B. For example, if A is five and B is 10, your ratio will be 5/10. Solve the equation. Divide data A by data B to find your ratio.


1 Answers

"foo/bar/baz".enum_for(:scan, %r{/|$}).map {Regexp.last_match.pre_match}
like image 91
sepp2k Avatar answered Sep 21 '22 19:09

sepp2k