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).
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.
"foo/bar/baz".enum_for(:scan, %r{/|$}).map {Regexp.last_match.pre_match}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With